Rescue workers rushed to the worst scenes of destruction, in the southern states of Oaxaca and Chiapas, where officials warned there may be people trapped in the ruins.
President Enrique Pena Nieto described the 8.2 magnitude quake as "the largest registered in our country in at least the past 100 years" -- stronger even than a devastating 1985 earthquake that killed more than 10,000 people in Mexico City.
Twenty-five people were killed in Oaxaca alone, Mexico's agriculture secretary Jose Calzada said.
Officials said the death toll there could rise.
More From This Section
"There are houses that collapsed with people inside," Luis Felipe Puente, director general of the emergency response agency, told TV news channel Milenio.
A hotel also collapsed in Juchitan, the town hall partly caved in, and many homes were badly damaged.
Twelve people were killed in Chiapas and three people, including at least two children, were killed in neighboring Tabasco state, authorities said.
In Tabasco, one child was crushed by a collapsing wall. Another, an infant on a respirator, died after the quake triggered a power outage.
More than 200 people were injured across Mexico, officials said.
Four people were also injured in neighboring Guatemala.
The quake epicenter was about 100 kilometers from the coastal town of Tonala, in far southern Chiapas state. It hit at 11:49 pm (0449 GMT today), Mexico's seismologic service said.
The US Geological Survey put the magnitude slightly lower, at 8.1 -- the same as the 1985 quake, the country's most destructive ever.
Many of those fleeing were in their night clothes. Some clutched babies and pets as they stumbled onto the streets.
"Not another one. God, please no," said one woman, falling to her knees to pray.
"I was driving when the ground started to shake. The car was wobbling," said Cristian Rodriguez, a 28-year-old Uber driver in Mexico City.
Onelia Guerra, an anxious Oaxaca local who was in the capital, was taking a plane back to home to check on her family.
The quake struck as Mexicans were also bracing for the impact of Hurricane Katia, which strengthened to a Category Two storm as it rumbled towards the state of Veracruz on the Gulf coast.
It is one of three active hurricanes in the Atlantic Ocean alongside Irma and Jose.
Speaking from the National Disaster Prevention Center's headquarters, where he was supervising the emergency response, Pena Nieto said that 50 million people out of Mexico's population of 120 million people felt the quake.
The president, who was due to travel to Chiapas later today, tweeted that the country's ports and airports had resumed normal operations.
The quake struck at a depth of 69.7 kilometers, according to the USGS.
Initially, authorities issued a tsunami alert for a huge stretch of coastline starting in central Mexico and spanning Central America all the way down to Ecuador.
It was later lifted, but Mexico remained on alert for aftershocks. There have been more than 260 so far, Pena Nieto said.
Since the 1985 earthquake, Mexican authorities have instituted a stricter building code and developed an alert system using sensors placed on the coasts.