Relatives sobbed as firefighters pulled charred bodies from the incinerated homes, some of the remains barely more than piles of ashes and bones.
Jose Luis Chavez, 58, who lives 10 blocks from the explosion, said he heard at least two loud booms and saw flames leap more than 30 feet (10 meters) in the air. He said it was as if a bomb had exploded underground.
"The explosions we had were very scary," Chavez said.
Aside from the deaths, at least 52 people were hurt and at least 200 were in shelters after fleeing San Martin, which is about 55 miles (90 kilometers) east of Mexico City. More than 115 homes were scorched, 30 of them destroyed.
The explosion, which happened before dawn Sunday, was apparently caused by thieves trying to steal crude oil, said Valentin Meneses, interior secretary for the state of Puebla, where San Martin is located. Investigators found a hole in the pipeline and equipment for extracting crude, said Laura Gurza, chief of the federal Civil Protection emergency response agency.
"They lost control because of the high pressure with which the fuel exits the pipeline," he said, adding that the oil began to flow down the city's streets and into a nearby river.
Several bodies were found in cars near the location of the leak, but authorities didn't know if the dead were involved in the theft or just there by coincidence. The rupture occurred in an elevated part of the city, sending the crude running down a river bed more than a half mile (1 kilometer), Gurza said.
At some point a spark caused the crude to erupt into flames, though officials didn't know the origin of the spark.
Many of the buildings destroyed were humble two-story cement homes. Gurza said people are not permitted to live near oil pipelines, but Chavez and others indicated they had been in the area for some time.
President Felipe Calderon expressed his condolences to the families of the dead and his support for those injured and affected. He said the federal government would give its full support in investigating who was responsible and bringing them to justice.
Interior Minister Francisco Blake Mora mobilized several federal ministries to help victims with medical care, shelter and recovering their lost homes and property.
The state-owned oil company Petroleos Mexicanos, or Pemex, said in a statement that it had shut down the pipeline. Government authorities said the fire was under control by midday, and fires were burning the remaining crude. The area was without electricity or water.
San Martin Texmelucan is a city of about 130,000 people, according to 2005 government figures. According to the city's website, farming is important to the area's economy, along with a manufacturing sector that makes chemical and petrochemical products, pharmaceuticals, textiles and metals.
Civil protection authorities, firefighters and military officers were investigating to make sure there are no more explosions. No one had been detained.
Pemex has struggled with chronic theft, losing as much as 10 percent of all of its product.
Criminals tap remote pipelines, sometimes building pipelines of their own, to siphon off hundreds of millions of dollars worth of oil each year, Pemex has said.
In 2009, the U.S. Justice Department said U.S. refineries bought millions of dollars worth of oil stolen from Mexican government pipelines and smuggled across the border in illegal operations led by Mexican drug cartels expanding their reach.
Two Texas oil company officials were sentenced to probation in September for their roles in the sale of petroleum products stolen from Mexico.
Pemex sued five companies in the U.S. in June for allegedly buying stolen Mexican petroleum products.
Also in June, police arrested 13 people who they said excavated a 500-foot (150-meter) tunnel under a busy neighborhood in Mexico City to steal fuel from oil company pipelines.