Lewis Hamilton will start the 2017 Azerbaijan Grand Prix from pole position ahead of Mercedes team-mate Valtteri Bottas, after putting in a last-second flyer.
The pole is the Britain's 66th in Formula 1, putting him just two shy of the all-time record set by Michael Schumacher.
Hamilton had it all to do as the clock ticked down in Q3 after sitting in P2 behind Bottas after the first runs, having run wide through Turn 16 on his initial effort.
Daniel Ricciardo hit the wall at Turn 6 with a few minutes remaining, bringing out the red flags, though there was sufficient time left for the nine surviving cars to make a last minute dash to improve on their respective efforts.
Bottas improved his time to post a 1:41.027, crossing the line just as the clock ticked down to zero, but Hamilton behind smashed that time by 0.434 seconds to go P1.
Kimi Räikkönen also improved his time, but Ferrari had no answer for Mercedes' pace. Räikkönen will line up third ahead of team-mate Sebastian Vettel, the pair more than a second adrift.
Friday pacesetter Max Verstappen was the first out when the track went green following his team-mate's crash and managed to get in two laps, but neither were good enough to secure him a higher grid position than fifth.
The Force Indias of Sergio Pérez and Esteban Ocon were sixth and seventh respectively, whilst rookie Lance Stroll outqualified his Williams team-mate Felipe Massa to bag eighth spot.
Ricciardo completed the top ten, unable to continue in the session after his contact with the wall.
The Toro Rosso cars claimed row six – but Carlos Sainz Jr. will drop three-places on the grid thanks to his Canadian GP penalty – ahead of Haas' Kevin Magnussen and Renault's Nico Hulkenberg.
The second Renault of Jolyon Palmer failed to take part in the session after suffering an engine fire in final practice, which the team were unable to change in time.