Hamilton finished second, with Valtteri Bottas completing the podium for Williams.
Rosberg, who dominated practice and qualifying,
held off Hamilton after a Sebastian Vettel crash saw the safety car deployed on lap 53 of 71 at the Autodromo Hermanos Rodriguez.
A late mistake in a similar situation in the previous round last Sunday had seen Hamilton win the United States Grand Prix to wrap up his second straight title.
But Rosberg was clearly determined to avoid a repetition and it was errors by Hamilton which left him unable to claim his fourth straight win.
It has also cost him the chance to equal the record wins for an F1 season of 13, with 10 under his belt but only two rounds to go in 2015 in Brazil and Abu Dhabi.
The Red Bull pair of Daniil Kyvat and Daniel Ricciardo drove fine races for fourth and fifth, but the biggest cheers for a capacity and fervent crowd were reserved for home hero Sergio Perez in eighth for Force India.
Raucous scenes greeted the drivers at the end of an enthralling race, enlivened by Vettel's collision into a barrier, the four-time champion walking away unscathed.
It completed a miserable afternoon for the Ferrari ace, who has seen Rosberg leapfrog him into second place in the standings behind Hamilton.
Donning a sombrero, Rosberg told Nigel Mansell the winner of the last Mexico Grand Prix in 1992, that it was a memory which would live long with him.
"Amazing day, a great battle with Lewis, who drove really well. I'm really happy with the win, what a place to do it.
"An absolutely awesome crowd."
Hamilton had a brief disagreement with his team over race radio at the second round of pit stops, hinting it had compromised his victory charge, but all was forgotten at the presentation.
"Nico drove a great race," he said before paying his own tribute to the fans.
"This crowd is like being at a football game. The fans have been amazing. I've never seen anything like this."