Fernando Alonso became the first man to win two races this year with a spectacular victory in the European GP.
The Spaniard won his home race at Valencia after a series of overtakes and Sebastian Vettel's retirement.
Lewis Hamilton was set to finish second after Romain Grosjean's retirement, but crashed out with three laps remaining.
The McLaren driver was pitched into the barriers after a tussle with Williams's Pastor Maldonado, handing second to Lotus's Kimi Raikkonen.
Alonso started 11th
and fought his way up to fourth place before a safety car was deployed.
My most emotional win - Alonso
He gained third after a pit-stop problem for Lewis Hamilton, passed Romain Grosjean on the restart and saw leader Vettel retire with alternator failure on lap 35.
Battling fading tyres, Hamilton was passed by Raikkonen before Maldonado crashed into the side of him having rejoined the race track after running wide trying to pass the Briton around the outside.
Grosjean retired from second with 16 laps to go with alternator failure - the same problem that caused Vettel to retire.
The frantic action promoted Mercedes's Michael Schumacher to third place - the first podium since his comeback in 2010 - and Red Bull's Mark Webber to fourth from 19th on the grid.
It was by far the most exciting race the Valencia street circuit has ever produced and it was won by a top-drawer performance from Alonso.
BBC F1 co-commentator David Coulthard said: "A truly world class drive - and bitter disappointment for Lewis Hamilton after a tap by Maldonado, who T-boned him after running wide."
Few would have predicted the result in the early stages as Vettel, who started from pole position, streaked away from the field at the front and pulled out more than five seconds in the first three laps.
European GP result
1. Fernando Alonso 1:44:16.649
2. Kimi Raikkonen +00:06.421
3. Michael Schumacher +00:12.639
4. Mark Webber +00:13.628
5. Nico Hulkenberg +00:19.993
6. Nico Rosberg +00:21.176
7. Paul Di Resta +00:22.866
8. Jenson Button +00:24.653
9. Sergio Perez +00:27.777
10. Pastor Maldonado +00:34.630
Vettel controlled the race, building a lead of 20 seconds over Grosjean, before the safety car was deployed on lap 30, with 26 to go, because of debris on the track following a collision between Caterham's Heikki Kovalainen and Toro Rosso's Jean-Eric Vergne.
Meanwhile, Alonso was driving one of the best races of his illustrious career, moving up to eighth on the first lap and taking seventh from Force India's Nico Hulkenberg on lap 12.
Two quick laps before his own pitstop while the leaders in front of him made theirs promoted him to a de facto fourth behind Vettel, Grosjean and Hamilton.
All the leaders pitted for a second time when the safety car came out, and Alonso - who had been closing on Hamilton at approaching half a second a lap - gained another place following the latest in a series of pit-stop problems for Hamilton after his front jack failed.
He then passed Grosjean
around the outside of Turn 1 on the restart
- a move he had used to pass Mark Webber's Red Bull earlier in the race - and then took the lead when Vettel's engine stopped further around the same lap.
European GP facts
- Track temperature:
- Air temperature:
- Ave wind speed:
1.6 metres per second
- Fastest lap:
Nico Rosberg (lap 54): 1 min 42.163 secs
- Fastest Speeds:
- Sector 1:
Jenson Button 285kph (177.09mph)
- Sector 2
: Nico Rosberg 306kph (190.13mph)
- Sector 3
: Fernando Alonso 243kph (150.99mph)
- Speed Trap:
M Schumacher 320kph (198.83mph)
"To be honest we don't know yet [what happened]," Vettel said. "I lost drive on the straight down to Turn 17 and I had to give way to the other people. The engine stalled and switched off."
Hamilton's engineers urged him on, believing Alonso's tyres would wear out faster than theirs, but the double world champion was always in control.
The 29th victory of Alonso's career and extraordinary result moves the 30-year-old back into the championship lead, 20 points ahead of Webber. Hamilton is third, 23 points adrift and Vettel fourth, now 26 points - more than a win - behind Alonso.
It was a gripping battle as the combination of the fast-wearing Pirelli tyres and a perfectly judged DRS overtaking zone finally cracked the problem of overtaking at Valencia.
There were fights up and down the field, positions changing and collisions.
"One of the things that makes Alonso so good is that if he gets an opportunity to win, he will take it. That is exactly what happened in Valencia."
Read more from Gary Anderson
Paul di Resta went into the final four laps in sixth place, but his valiant attempt to make only one pit stop left him vulnerable in the closing laps and he lost places to Schumacher, Webber and Mercedes' Nico Rosberg.
Di Resta finished seventh, ahead of Hamilton's McLaren team-mate Jenson Button, Sauber's Sergio Perez and Maldonado, who may well face a stewards' inquiry for the incident with Hamilton.
Sauber's Kamui Kobayashi has been given a five-place penalty for the British Grand Prix following his collision with Felipe Massa, while Vergne has been given a 10-place penalty and £20,000 fine after crashing into Kovalainen.
European Grand Prix 2012, day three
- Sunday, 24 June: Race highlights 23:45-00:45; BBC Two.