Daniel Ricciardo made two late overtaking moves on Lewis Hamilton and Fernando Alonso to claim the second victory of his Formula 1 career in a dramatic Hungarian Grand Prix.
Alonso looked set to end a 15-month victory drought for Ferrari, but ultimately found a mammoth 32-lap final stint on a set of soft Pirelli tyres too much and thus had to settle for second.
Hamilton reduced the points gap to title rival Nico Rosberg by taking an unlikely podium finish after a pitlane start, just fending off his Mercedes team-mate over the final lap as he battled to the finish on a well-worn set of medium tyres.
Hamilton survived a brush with the barrier after spinning at Turn 2 on the first lap, and also courted intra-team controversy by holding up team-mate Rosberg during his final stint despite radio requests to let him through.
Rosberg dropped to seventh after his third and final stop with 14 laps to go, but charged back to fourth to minimise the damage inflicted by two-stopper Hamilton to his championship lead to just three points.
Poleman Rosberg had dominated the wet early stages, leading Valtteri Bottas, Sebastian Vettel and the fast-starting Ferrari of Alonso, but this quartet had to wait an extra lap before pitting for dry tyres when Marcus Ericsson crashed heavily exiting Turn 3 on lap nine, bringing out the safety car.
This dropped them into the pack behind Ricciardo's Red Bull and the McLarens of Jenson Button and Kevin Magnussen, and not far ahead of Hamilton, who had made rapid progress through the midfield.
McLaren gambled on more rain, so fitted Button with new intermediate tyres and left Magnussen out on his original set. This gambled failed and the McLarens were forced to pit for dry tyres in racing conditions.
A huge crash for Sergio Perez (who had earlier collided with Force India team-mate Nico Hulkenberg) exiting the final corner on lap 23 brought the safety car back out, and Ricciardo and the Williamses of Bottas and Felipe Massa dived for the pits.
This left Alonso in the lead, but the Spaniard's gamble on a two-stop strategy failed as Ricciardo charged back to the head of the field over the final few laps.
Massa ran as high as second, but switched to a three-stop strategy and made it home fifth, ahead of Kimi Raikkonen, who scored his best result since returning to Ferrari this season by rising from 16th on the grid to sixth.
Vettel ran in the top order early on, but spoiled his race with a frightening 360-degree spin after catching the wet kerb exiting the final corner on lap 32.
The reigning world champion ultimately brought his Red Bull home seventh, ahead of Bottas (who lost out badly in the pits) and the Toro Rosso of Jean-Eric Vergne (who also ran at the front early on).
Button recovered from McLaren's early strategic blunder to claim the final point for 10th.
Results - 70 laps:
Pos Driver Team/Car Time/Gap
1. Daniel Ricciardo Red Bull-Renault 1h53m05.058s
2. Fernando Alonso Ferrari +5.225s
3. Lewis Hamilton Mercedes +5.857s
4. Nico Rosberg Mercedes +6.361s
5. Felipe Massa Williams-Mercedes +29.841s
6. Kimi Raikkonen Ferrari +31.491s
7. Sebastian Vettel Red Bull-Renault +40.964s
8. Valtteri Bottas Williams-Mercedes +41.344s
9. Jean-Eric Vergne Toro Rosso-Renault +58.527s
10. Jenson Button McLaren-Mercedes +1m07.280s
11. Adrian Sutil Sauber-Ferrari +1m08.169s
12. Kevin Magnussen McLaren-Mercedes +1m18.465s
13. Pastor Maldonado Lotus-Renault +1m24.024s
14. Daniil Kvyat Toro Rosso-Renault -1 lap
15. Jules Bianchi Marussia-Ferrari -1 lap
16. Max Chilton Marussia-Ferrari -1 lap
Esteban Gutierrez Sauber-Ferrari 33 laps
Kamui Kobayashi Caterham-Renault 25 laps
Sergio Perez Force India-Mercedes 23 laps
Nico Hulkenberg Force India-Mercedes 15 laps
Romain Grosjean Lotus-Renault 11 laps
Marcus Ericsson Caterham-Renault 8 laps
1. Nico Rosberg 202
2. Lewis Hamilton 191
3. Daniel Ricciardo 131
4. Fernando Alonso 115
5. Valtteri Bottas 95
6. Sebastian Vettel 88
7. Nico Hulkenberg 69
8. Jenson Button 60
9. Felipe Massa 40
10. Kevin Magnussen 37
11. Sergio Perez 29
12. Kimi Raikkonen 27
13. Jean-Eric Vergne 11
14. Romain Grosjean 8
15. Daniil Kvyat 6
16. Jules Bianchi 2
17. Adrian Sutil 0
18. Marcus Ericsson 0
19. Pastor Maldonado 0
20. Esteban Gutierrez 0
21. Max Chilton 0
22. Kamui Kobayashi 0
1. Mercedes 393
2. Red Bull-Renault 219
3. Ferrari 142
4. Williams-Mercedes 135
5. Force India-Mercedes 98
6. McLaren-Mercedes 97
7. Toro Rosso-Renault 17
8. Lotus-Renault 8
9. Marussia-Ferrari 2
10. Sauber-Ferrari 0
11. Caterham-Renault 0