Lewis Hamilton took pole position for the Mexican Grand Prix as his points leading teammate Nico Rosberg left it until the dying seconds to join him on the front row.
Rosberg had looked set to start behind the Red Bull duo of Max Verstappen and Daniel Ricciardo, but a last-gasp effort go him to within a quarter of a second of Hamilton's pole time.
Verstappen will start third, ahead of Ricciardo.
Both Mercedes and Ferrari of Sebastian Vettel (who qualified seventh) will start the race on the soft tyre, after using it to progress from Q2, while both Red Bulls and Ferrari's Kimi Raikkonen (sixth) will start on supersofts.
Story of the Q3 shootout
With the asphalt temperature soaring to 54C, the track was busy right from the start. Verstappen – with Red Bull only taking one out lap to switch on the supersofts, unlike Mercedes – was first to set a benchmark P1 time with a 1m19.092s with Ricciardo on 1m19.210s.
Hamilton then took provisional pole with a 1m18.704s, with Rosberg only managing a 1m19.263s for P4 behind both Red Bulls.
Vettel's first attempt was only good for eighth, 1.3s off the pace.
On the second runs, Verstappen got to within 0.35s of Hamilton's pole time but Rosberg pulled one out of the back, a 1m18.958s, to get within 0.254s of Hamilton's time.
Hamilton managed a 1m18.710s on his second run, just shy of his best time.
Nico Hulkenberg was an impressive fifth for Force India, ahead of Raikkonen.
Vettel improved his time but was only seventh fastest, 0.677s off the pace.
Valtteri Bottas will start eighth for Williams, ahead of teammate Felipe Massa, with Carlos Sainz rounding out the top 10 for Toro Rosso.
Sainz wins Spanish battle in Q2
Most top teams opted to run on soft-compound tyres at the start of Q2, apart from Red Bull, who went straight to supersofts.
Hamilton had to pass a weaving Vettel at the final corner on his first lap – prompting a "Got blocked by Vettel" over the radio, but the Briton still set 1m19.137s on softs. Vettel waved him an apology on the following straight.
Verstappen took the top spot with 1m18.972s on supersofts, 0.165s quicker than Hamilton's time. Vettel was third on softs, saying he felt they gave "better grip" than the supersoft. Ricciardo only managed fourth on his supersofts, ahead of Rosberg, who required two runs on softs.
Hulkenberg was best of the rest in sixth, ahead of a supersoft-shod Raikkonen, with Williams duo Bottas and Massa in eighth and ninth.
Sainz grabbed the last spot in Q3, demoting McLaren's Fernando Alonso.
Local hero Sergio Perez will start 12th in his home race, after he locked up at Turn 1 on his final run, ahead of the second McLaren of Jenson Button.
Kevin Magnussen will start 14th for Renault, ahead of Sauber's Marcus Ericsson and the Manor of Pascal Wehrlein.
Disaster for Haas in Q1
Haas's Esteban Gutierrez spun on his last Q1 lap at Turn 11, and was knocked out from potential Q2 progression by Wehrlein seconds later.
Gutierrez's spin delayed teammate Romain Grosjean, who had already been off at Turn 12 and also suffered a battery issue on his first run.
Daniil Kvyat reported a power issue with his Toro Rosso, just as he did at the end of FP3, and wasn't able to run again. That consigned him to 18th, ahead of Sauber's Felipe Nasr, Esteban Ocon's Manor and the disgruntled Grosjean.
Renault's Jolyon Palmer will start last after missing qualifying due to the discovery of a cracked chassis.
Hamilton topped the session on 1m19.554s on his fifth lap using supersofts, ahead of Raikkonen, Ricciardo, Vettel, Verstappen and Rosberg.
|3||Max Verstappen||Red Bull||TAG||1'19.054||0.350|
|4||Daniel Ricciardo||Red Bull||TAG||1'19.133||0.429|
|5||Nico Hulkenberg||Force India||Mercedes||1'19.330||0.626|
|10||Carlos Sainz Jr.||Toro Rosso||Ferrari||1'20.378||1.674|
|12||Sergio Perez||Force India||Mercedes||1'20.287||1.583|
|18||Daniil Kvyat||Toro Rosso||Ferrari||1'21.454||2.750|