The reigning world champion Lewis Hamilton achieved a superb pole position in a rain-hit qualifying for the Hungarian Grand Prix.
The Q3 drivers used wet tyres throughout, and on the first runs Kimi Raikkonen had the advantage over Hamilton.
But after both Mercedes drivers pitted for a second set of wets with three-and-a-half minutes remaining, it turned into a battle between Hamilton and team-mate Valtteri Bottas.
Bottas had the advantage after the first two sectors, but Hamilton was a stunning 0.426 seconds faster in the final sector to take pole by 0.260 seconds.
Raikkonen reclaimed third place from team-mate Sebastian Vettel late on by just 0.024 seconds to make it an all-Ferrari second row.
Renault driver Carlos Sainz Jr was the only driver not to use two sets of wets in Q3 and took fifth position ahead of the Toro Rosso of Pierre Gasly.
Red Bull had a poor session, with Daniel Ricciardo not even reaching Q3 and Max Verstappen down in seventh place and 2.374 seconds off pole.
Brendon Hartley was a career-best eighth, ahead of Haas pairing Kevin Magnussen and Romain Grosjean.
Q2 started with drivers heading out on slicks but following light rainfall – with Vettel on intermediates the only exception.
With the rain returning and intensifying, Vettel set a lap 2.1 seconds faster than everyone while the rest of the field dived back into the pits for intermediates.
Those who were able to get back in and change the quickest benefited in terms of track conditions as the circuit gradually got wetter.
Fernando Alonso, who was not one of the last to head out, ended up P11 and was unable to improve after taking on wets later in the session.
But that put him ahead of Ricciardo, one of the later drivers to set his initial time on intermediates and who ended up in P12 having marginally improved after bolting on wets.
Ricciardo was delayed by yellow flags for Lance Stroll’s off at Turn 9, although he also lost time in other parts of the lap compared to Raikkonen – who set a time over four seconds faster while running around 11 seconds behind.
Nico Hulkenberg, also among the later runners, was P13 ahead of Sauber’s Marcus Ericsson.
Williams driver Stroll made it to Q2, but spun coming out of Turn 9 and nosed into the barrier, bringing his session to an end.
McLaren driver Stoffel Vandoorne was the quickest of those eliminated in a frenetic wet/dry Q1 session after being shuffled back due to late improvements from the other drivers.
Initially, everyone was running the intermediate Pirellis following a downpour before the start, but even early in the session they were looking to look to slicks.
Vandoorne was out of the dropzone late on but he was one of the first to complete his final lap on a track that was getting quicker.
Ricciardo, Ericsson, Hartley and Stroll were among the drivers to make late improvements and push Vandoorne into the bottom five.
Charles Leclerc was P17 for Sauber, 0.035 seconds slower than Vandoorne but comfortably clear of the lead Force India of Esteban Ocon.
Ocon did his best lap on his final time round, but it was half-a-second away from being enough for a Q2 place as he was struggling with a lack of rear brakes.
Team-mate Sergio Perez was P19, a tenth clear of Williams driver Sergio Sirotkin, who complained about being delayed by a Ferrari in the final two corners on his quick lap.
So congratulations to Lewis Hamilton and Mercedes in taking pole position. The rain certainly helped in terms of balancing out the performance as Ferrari looked so strong and fast in the earlier session. Bring on the race.
Hungarian Grand Prix, qualifying positions:
1 Lewis Hamilton Mercedes 1m35.658s
2 Valtteri Bottas Mercedes 1m35.918s
3 Kimi Raikkonen Ferrari 1m36.186s
4 Sebastian Vettel Ferrari 1m36.210s
5 Carlos Sainz Renault 1m36.743s
6 Pierre Gasly Toro Rosso-Honda 1m37.591s
7 Max Verstappen Red Bull-Renault 1m38.032s
8 Brendon Hartley Toro Rosso-Honda 1m38.128s
9 Kevin Magnussen Haas-Ferrari 1m39.858s
10 Romain Grosjean Haas-Ferrari 1m40.593s
11 Fernando Alonso McLaren-Renault 1m35.214s
12 Daniel Ricciardo Red Bull-Renault 1m36.442s
13 Nico Hulkenberg Renault 1m36.506s
14 Marcus Ericsson Sauber-Ferrari 1m37.075s
15 Lance Stroll Williams-Mercedes –
16 Stoffel Vandoorne McLaren-Renault 1m18.782s
17 Charles Leclerc Sauber-Ferrari 1m18.817s
18 Esteban Ocon Force India-Mercedes 1m19.142s
19 Sergio Perez Force India-Mercedes 1m19.200s
20 Sergey Sirotkin Williams-Mercedes 1m19.301s