Rosberg, leading Hamilton by 26 points in the title race, arrived here in Mexico knowing that a win, and 10th or lower for Hamilton, would be enough for him to clinch the championship.
But the German, who has been strangely out of sorts this weekend, was staring at a disappointing fourth on the grid only to save his very best lap of the weekend until the closing moments to join Hamilton on the front row.
The Red Bull duo of Max Verstappen and Daniel Ricciardo will start from third and fourth respectively.
With the championship race reaching its final crescendo, Rosberg, a winner in Mexico last year, has been well off the pace of Hamilton throughout the weekend.
Hamilton was comfortably faster than the German in all three practice sessions, and while his 10th pole of the season rarely looked in doubt, it appeared as though Red Bull would throw a spanner in the title works.
Hamilton can win each of the three remaining races, and still miss out on the championship. Indeed Rosberg can afford to finish second on two occasions, and third once, to claim his maiden title.
And at one stage Rosberg was on course to start behind not only Hamilton, but Verstappen and Ricciardo, too, before he pulled out his fastest lap to pip the Red Bull pairing. Rosberg, who was 0.2 seconds slower than Hamilton, will be relieved to join his Mercedes team-mate on the front row.
Hamilton, who is chasing his second victory in seven days following his commanding display in America, said: “I turned up to do the job and I’m going to try to do the same thing as I did last weekend.
“So far this weekend has gone well and I’m looking forward to the race. The long runs seemed good and the car feels great, so just looking forward to getting out on track.”
Rosberg added: “It’s not pole position, but it gives me great chance for tomorrow anyway.
“It took a bit longer this weekend to find the way. Just generally with tyre temperatures, it being on the cold side all the weekend. So it’s a bit nervous out there and just to find the way with that took a bit longer. But got there in the end, so it was okay.”
Aside from the championship battle, Jolyon Palmer, the British rookie who is bidding to prove to his Renault bosses he deserves a second season with the team, will start from the back of the field today after he was ruled out of qualifying with a cracked chassis.
Renault discovered the problem after final practice yesterday morning, but they were unable to resolve the issue in time for qualifying.
“I won’t be taking part in quali after we found a crack in the chassis,” Palmer tweeted. “Big shame as we were 14th in FP3 and aiming for Q2 again.”
His team-mate Kevin Magnussen qualified 14th, one place behind Jenson Button for McLaren. The 2009 world champion, who will bring to an end his run of 17 consecutive seasons in Abu Dhabi next month, was four tenths adrift of his team-mate Fernando Alonso, who qualified 11th.
Elsewhere, Nico Hulkenberg was fifth for Force India, while the Ferrari pair of Kimi Raikkonen and Sebastian Vettel, who had threatened to take the challenge to Mercedes, were sixth and seventh.