Lewis Hamilton produced a stunning last-gasp lap in qualifying for the Azerbaijan Grand Prix to beat team-mate Valtteri Bottas to pole.
The Briton made a mistake on his first lap when he looked set to go fastest and a red flag looked like it might have cost him a chance of pole.
The session was restarted with enough time for one lap and Hamilton beat his Mercedes team-mate by 0.434 seconds.
Ferrari’s Kimi Raikkonen and Sebastian Vettel will start third and fourth.
An anxious wait for Hamilton – and then boom
Hamilton had struggled through the practice sessions in Baku but found his form at the start of qualifying and was comfortably quickest in both the first and second sessions.
But on his first lap in the top 10 shootout Hamilton ran wide at the final real corner, Turn 16, on to the long pit straight and lost half a second in the final sector as a result. He was just 0.15secs behind Bottas.
Hamilton was planning a second lap when Red Bull’s Daniel Ricciardo slid into the wall at Turn Six and forced the session to be stopped while marshals recovered his car.
There were just three and a half minutes left – time for just one more lap when it takes two minutes to get out of the pits and round to the start line on F1’s second longest track.
But in a thrilling conclusion, Hamilton produced a peach of a lap that gave him a big margin over the field.
“That was one of the most exciting laps I’ve had all year and a lot of pressure,” Hamilton said.
“The first lap I easily had the time but I made a mistake in the last corner. It was all or nothing but the lap just got better and better throughout.
“I saw Valtteri ahead and knew he was doing a good lap. I was like, ‘please be enough’. I am ecstatic.
“I am so pumped with that. That’s how qualifying should be. It is going to be a long, hard race but we are in the best position to start.”
Ferrari fight back
Vettel, who starts the race 12 points ahead of Hamilton in the championship, salvaged a decent position in fourth after a difficult day.
He lost most of final practice because of a hydraulics problem and Ferrari had to do a last-minute engine change before qualifying.
Ferrari fitted an old-specification engine, the first one he used this season, which has less power than the newer engine he was meant to use, and Vettel was never close to the Mercedes pace.
Despite the extenuating circumstances, Ferrari may well be concerned by the size of the margin to Mercedes – Hamilton was 1.1secs quicker than Raikkonen, with Vettel 0.148secs behind his team-mate.
Vettel said: “We were not quick enough. The gap to Mercedes in front was bigger than anyone expected.”
But he added that he expected to be able to challenge for the win.
“Mercedes will also be quick, but I have no doubts that we can be a match,” he said.
Red Bull challenge fades
For much of qualifying, Red Bull’s Max Verstappen looked the closest challenger to Mercedes, but he was left breathless as the top two teams turned up their engines into qualifying mode.
Verstappen said he had lost 0.3secs on his final lap because of a gear shift problem on the main straight and felt he could have been third fastest.
The two Force Indias were next, Sergio Perez just pipping Esteban Ocon by 0.075secs, and behind them rookie Lance Stroll out-qualified Williams team-mate Felipe Massa for the first time this season, by just 0.045secs.
More woe for McLaren
McLaren already knew they would be starting from the back after a plethora of engine penalties for both drivers even before the weekend started.
But they did not even have the consolation of a decent performance on track – Fernando Alonso was knocked out in first qualifying for the first time this season in 16th place, with team-mate Stoffel Vandoorne qualifying slowest, 0.7secs behind.
The only glimmer of good news was that they will not be right at the back – Jolyon Palmer will be behind them after not being able to take part in qualifying because of an engine fire in final practice.