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.
After equalling Ayrton Senna’s pole record in Canada, Lewis Hamilton is now just two behind Michael Schumacher’s all-time record of 68.
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.
Some corners of the tight and twisty circuit in Baku are only 7.6 metres wide
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.
Baku mixes the old and new
Max Verstappen hangs out with Baku’s musical guests the Black Eyed Peas
Azerbaijan has similar vantage points as Monaco for fans to take pictures of the action
Sergio Perez crashed in the same place as Jolyon Palmer in the first practice session on Friday