The quake was apparently stronger than a devastating 1985 temblor that flattened swathes of Mexico City and killed thousands, but this time, damage to the city was limited.
A number of buildings suffered severe damage in parts of southern Mexico. Some of the worst initial reports came from the town of Juchitan in Oaxaca state, where sections of the town hall, a hotel, a bar and other buildings were reduced to rubble.
Alejandro Murat, the state governor, said 23 deaths were registered in Oaxaca, 17 of them in Juchitan.
A spokesman for emergency services said seven people were also confirmed dead in the neighboring state of Chiapas. Earlier, the governor of Tabasco, Arturo Nunez, said two children had died in his state.
The US Geological Survey (USGS) said the 8.1 magnitude quake had its epicenter in the Pacific Ocean, 54 miles (87 km) southwest of the town of Pijijiapan in the impoverished southern state of Chiapas, at a depth of 43 miles.
Rescue workers labored through the night in badly affected areas to check for people trapped in collapsed buildings.
Windows were shattered at Mexico City airport and power went out in several neighborhoods of the capital, affecting more than one million people. The cornice of a hotel came down in the southern tourist city of Oaxaca, a witness said.
The tremor was felt as far away as neighboring Guatemala.
The quake triggered waves as high as 2.3 ft (0.7 m) in Mexico, the Pacific Tsunami Warning Center said. Mexican television showed images of the sea retreating about 50 meters, and authorities evacuated some coastal areas.
President Enrique Pena Nieto said the tsunami risk on the Chiapas coast was not major.
"We are alert," he told local television.
More aftershocks were likely, the president said, advising people to check their homes and offices for structural damage and for gas leaks. The USGS reported multiple aftershocks, ranging in magnitude from 4.3 to 5.7.
Classes were suspended in most of central and southern Mexico on Friday to allow authorities to review damage.
There was no tsunami threat for American Samoa and Hawaii, according to the U.S. Tsunami Warning System. The national disaster agency of the Philippines put the country's eastern seaboard on alert, but no evacuation was ordered.
People in Mexico City, one of the world's largest cities, ran out into the streets in pajamas and alarms sounded after the quake struck just before midnight, a Reuters witness said.
Helicopters buzzed overhead a few minutes later, apparently looking for damage to buildings in the city, which is built on a spongy, drained lake bed.
"I had never been anywhere where the earth moved so much. At first I laughed, but when the lights went out, I didn't know what to do," said Luis Carlos Briceno, an architect, 31, who was visiting Mexico City. "I nearly fell over."
In one central neighborhood, dozens of people stood outside after the quake, some wrapped in blankets against the cool night air. Children were crying.
Liliana Villa, 35, who was in her apartment when the quake struck, fled to the street in her nightclothes.
"It felt horrible, and I thought, 'this (building) is going to fall.'"
State oil company Pemex said it was still checking for damage at its installations.
Pena Nieto said operations at the Salina Cruz refinery in the same region as the epicenter were temporarily suspended as a precautionary measure.