At least 12 people were killed in the neighbouring state of Mexico, and three deaths were reported in the state of Guerrero on the Pacific coast.
The magnitude 7.1 quake toppled dozens of buildings, broke gas mains and sparked fires less than two weeks after another powerful quake killed at least 98 people in southern Mexico. It also hit just hours after emergency drills marked the anniversary of a temblor that killed thousands in 1985.
Millions of people fled into the streets, where they weathered the violent shaking and desperately sought word about the welfare of family and friends.
Emergency personnel in Mexico City, a metropolitan region of about 20 million people, searched frantically with picks and shovels for survivors beneath the rubble of what the sprawling city's mayor calculated to be as many as 44 collapsed buildings, including at least one primary school.
Mexican President Enrique Pena Nieto said more than 20 children and two adults had been found dead at the school, Colegio Enrique Rebsamen, in the neighbourhood of Coapa. Another 30 children and 12 adults were missing, he said.
Emergency personnel and equipment were being deployed across affected areas so that "throughout the night we can continue aiding the population and eventually find people beneath the rubble," Pena Nieto said in a video posted on Facebook.
Rescue workers and soldiers toiled around collapsed buildings where heat-sensing equipment suggested survivors could still be trapped.
Bystanders joined in where they could, clearing debris with their bare hands or whatever tools they could find nearby.
As many as 4.6 million homes, businesses and other facilities had lost electricity, according to national power company Comision Federal de Electricidad. Most of them were in the greater Mexico City area and in the states of Guerrero, Morelos, Puebla, Oaxaca, and Tlaxcala.
In the capital, ambulances and fire engines confronted gridlock as millions of workers tried to get home, many of them after participating in annual readiness drills that commemorate the previous disaster on this date in 1985.
Leaders have sent messages of support to Mexico.
US President Donald Trump, who has courted controversy with his plans for a border wall with Mexico, tweeted: "God bless the people of Mexico City. We are with you and will be there for you."
Canadian Prime Minister Justin Trudeau also tweeted his support following the "devastating news".
The Ministry of Foreign Affairs & Trade has urged New Zealanders living in Mexico City to contact the New Zealand embassy after a large earthquake there.
There are 117 New Zealanders registered on the SafeTravel website as living in Mexico City, according to MFat.
An MFat spokeswoman told NZ Newswire that the country's embassy in Mexico City is in contact with local authorities to determine whether any New Zealanders have been affected.
"All New Zealanders in Mexico are advised to register their details on safetravel.govt.nz," she said.
The NZinMexico Twitter feed advised that Kiwis in the affected area should follow the advice of the local authorities and monitor local media for developments.
"Please also let your family back in New Zealand know you're okay."
There have been 19 earthquakes of magnitude 6.5 or larger within 250km of Tuesday's quake in the past century, Earle said.
Earth usually has about 15 to 20 earthquakes this size or larger each year, Earle said.
Initial calculations show that more than 30 million people would have felt moderate shaking from the latest quake. The US Geological Survey predicts "significant casualty and damage are likely and the disaster is potentially widespread".