The US Geological Survey (USGS) said a 7.0 magnitude quake struck 11 miles northeast of the resort of Acapulco, Guerrero.
Mexico City Mayor Claudia Sheinbaum said there were no immediate reports of serious damage in the capital.
In the Roma Sur neighborhood of Mexico City, lights went off and scared residents rushed out, some wearing little more than pajamas, a Reuters witness said. Residents hurdled together in the rain, holding young children or pets, too worried to return to their homes in the dark.
"It was terrible. It really reminds me of the 1985 quake every time something like this happens," said Yesmin Rizk, a 70-year-old Roma Sur resident.
The USGS said the quake, initially measured at a magnitude of 7.4, was very shallow, only 7.8 miles (12.5 km) below the surface, which would have amplified the shaking effect.