The magnitude 7.5 quake was centred in the south-western state of Guerrero, close to the Pacific beach resort of Acapulco, the US Geological Survey (USGS) said.
Luis Felipe Puente, head of the Mexican government's emergency services, said on Twitter there were no immediate reports of damage and the US Pacific Warning Centre said it did not expect the quake to trigger a destructive tsunami.
Nevertheless, residents of the capital were shaken by the quake, one of the biggest to hit Mexico in several years.
"I had to hold on to a tree, like a drunk," said Pedro Hernandez, 68, a doorman working in central Mexico City.
The USGS said the quake was centred some 37km north of the municipality of Tecpan de Galeana in Guerrero. It was relatively shallow, at a depth of 49km.