The quake hit offshore in the Pacific at 11:49 pm (0449 GMT), about 100 kilometers (60 miles) from the coastal town of Tonala, in far southern Chiapas state, Mexico's seismologic service said.
"It was a major earthquake in scale and magnitude, the strongest in the past 100 years," said President Enrique Pena Nieto in an address from the National Disaster Prevention Center's headquarters, where he was supervising the emergency response.
The US Geological Survey put the magnitude slightly lower, at 8.1. That is the same as a devastating 1985 earthquake that killed more than 10,000 people in Mexico City -- the country's most destructive ever.
The quake shook a large swath of the country and was felt as far north as Mexico City -- some 800 kilometers from the epicenter -- where people ran from their homes as buildings trembled and swayed.
Pena Nieto said three people were killed in collapsing buildings in Chiapas.
In neighboring Tabasco state, two children were killed, the governor said.
One was crushed by a collapsing wall. The other, an infant on a respirator, died after the quake triggered a power outage.
The worst destruction appeared to be in Juchitan, in the state of Oaxaca, where 10 people were killed, according to the head of the emergency response agency, Ricardo de la Cruz.
Officials said the death toll there could rise.
"There are houses that collapsed with people inside," Luis Felipe Puente, the agency's director general, told TV news channel Milenio.
A hotel collapsed in Juchitan, the town hall partly caved in and many houses were badly damaged.
'The car was wobbling'
Pena Nieto said 50 million of Mexico's 120 million people felt the quake.
It was also felt in much of Guatemala, which borders Chiapas.
Mexican officials ordered schools to remain closed Friday in 11 states, including Mexico City, so officials could inspect for structural damage.
In the capital, people ran out of buildings -- many in their pajamas -- after hearing earthquake warning sirens go off just before midnight (0500 GMT Friday).
"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.
"We heard an explosion. Apparently it was a transformer. The streetlights started swinging back and forth," said Mayaro Ortega, 31, a resident of the capital's north side who went running from her building.
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.
But the alert was lifted several hours after the quake.
The Pacific Tsunami Warning Center had originally said a tsunami of more than three meters (10 feet) was possible.
In the end, the quake caused rough seas but no tsunami, officials said.
Since the 1985 earthquake, Mexican authorities have instituted a stricter building code and developed an alert system using sensors placed on the coasts.
Mexico sits atop five tectonic plates, whose movement makes it one of the most seismically active countries in the world.