Lewis Hamilton has taken pole position in a dramatic qualifying session for the Hungarian Grand Prix as title rival Sebastian Vettel lost out in rainy conditions.
With Ferrari appearing set for top spot in the dry, Mercedes made the most of the opportunity brought by the downpour as Hamilton took pole just ahead of teammate Valtteri Bottas.
The Briton headed Bottas by 0.260 seconds, while Vettel could manage only fourth, behind Ferrari teammate Kimi Raikkonen.
Crucially, Hamilton pitted for a fresh set of wet tyres with just minutes remaining before returning to the sodden tarmac and finding just enough grip to beat his opponents.
Renault's Carlos Sainz produced a strong performance in the wet, qualifying fifth after being second fastest in the second session.
The Spaniard was ahead of another impressive drive by Toro Rosso's Pierre Gasly, who beat Red Bull's Max Verstappen by 0.4 seconds.
Brendon Hartley ensured that two Toro Rosso cars would be in the top 10, ahead of Haas drivers Kevin Magnussen and Romain Grosjean.
Hungarian GP qualifying results
1. Lewis Hamilton (Mercedes)
2. Valtteri Bottas (Mercedes)
3. Kimi Raikkonen (Ferrari)
4. Sebastian Vettel (Ferrari)
5. Carlos Sainz Jr (Renault)
6. Pierre Gasly (Toro Rosso)
7. Max Verstappen (Red Bull)
8. Brendon Hartley (Toro Rosso)
9. Kevin Magnussen (Haas)
10. Romain Grosjean (Haas)
11. Fernando Alonso (McLaren)
12. Daniel Ricciardo (Red Bull)
13. Nico Hulkenberg (Renault)
14. Marcus Ericsson (Sauber)
15. Lance Stroll (Williams)
16. Stoffel Vandoorne (McLaren)
17. Charles Leclerc (Sauber)
18. Esteban Ocon (Force India)
19. Sergio Perez (Force India)
20. Sergey Sirotkin (Williams)