Daniel Ricciardo denied Max Verstappen the chance to become the youngest man in Formula One history to claim pole position after beating his Red Bull team-mate with a sensational flying lap in Mexico.

The rampant Red Bulls locked out the front row in a thrilling qualifying session, while Lewis Hamilton, who needs to finish only seventh on Sunday to claim the world championship, qualified third.

Ferrari's Sebastian Vettel, who must win here to stand any chance of stopping Hamilton from claiming this year's honours, finished behind his rival in fourth.

Verstappen, 21, had dominated all weekend at the Autodromo Hermanos Rodriguez, but he was beaten in the dying moments by Ricciardo, the Australian claiming his first pole at a track other than Monaco and only the third of his career.

Verstappen was clearly frustrated by the result, bumping the second-place board as he arrived on the start-finish straight.

Hamilton, who wants to see out the title which will draw him level with Juan Manuel Fangio with a victory, is in striking range of the Red Bulls but, crucially, ahead of his sole rival.

"I am really happy with it," Hamilton said after his Mercedes team had been well off the pace in practice on Friday.

"We had a difficult day yesterday and today was a big improvement. Being behind the Red Bulls is not a bad thing."

The 800-yard dash to the opening bend is the longest on the calendar and last year Vettel collided with Hamilton at the third bend.

"We saw what happened last year with the red car behind, so I don't know what will happen at the start tomorrow," he added.

"It depends how we get away, but I am going to be fighting to gain a position. I am wary of the Red Bulls ahead."

Ricciardo, who edged out Verstappen by just 0.026 seconds, was ecstatic with the result.

"I knew it was in there somewhere," he said moments after roaring over the radio in delight.

"I knew putting the lap together would be crucial as always, and Max showed the pace was in the car all weekend."

Verstappen was rather less excited.

"The whole qualifying was c**p," the deflated Dutchman said. "We had the same problems from practice yesterday.

"We tried to make the best of it and I thought it was going to be enough for pole with the problems we had."

Valtteri Bottas was fifth for Mercedes ahead of Ferrari's Kimi Raikkonen.

Fernando Alonso had been eliminated from Q1 at the previous three races, but the soon-to-be-departing double world champion provided some respite for McLaren by qualifying 12th. "Good job," the Spaniard was told.

His team-mate Stoffel Vandoorne, also leaving at the end of the year to be replaced by British teenager Lando Norris, finished 17th.

Home favourite Sergio Perez, whose laps through the unique stadium section were greeted with rockstar-like cheers, could manage only 13th, two places off Esteban Ocon in the sister Force India.