The conflagration in the Mexico City suburb of Tultepec set off a quick-fire series of multicolored blasts that sent a vast cloud of smoke billowing over the capital.
The market had been packed with customers buying pyrotechnics for traditional end-of-year festivities. Christmas and New Year parties in many Latin American countries often wrap up with clattering firework blasts.
"You just heard the blast. And everything started to be on fire. People came running out on fire," Walter Garduno said.
"People were alight -- children," he added before trailing off.
More From This Section
Of the 31 confirmed dead, "26 (died) at the scene and five in hospitals," local media reported Mexico's chief prosecutor Milenio Alejandro Gomez as saying.
Forensic experts are working on genetic analyses of the bodies because "almost all of them are impossible" to identify, Mexico state's governor Eruviel Avila told the Televisa television network.
At least 72 were wounded, the authorities said. The injured were transported to emergency rooms, and 21 have since been released.
The head of the civil protection service, Luis Felipe Puente, said crews had to wait for all the fireworks to finish exploding before they could extinguish the flames.
"The entire market is gone," he said. It had 300 stands.
Several of the injured were in "delicate condition," he added, saying searches were under way for more casualties in the scorched area that looked like a scene from a post-apocalyptic film, with little left standing in the smoldering ruins.
Homes and vehicles nearby were also severely damaged. In some areas, emergency workers were gently probing for survivors under heaps of charred and twisted roofing material.
Most of those picked up by rescuers suffered severe burns, many over their entire bodies.
The military, which is in charge of issuing fireworks sales permits, was deployed to help emergency crews transport casualties to hospitals by ambulance and helicopter.
Ambulances, fire trucks, police vehicles and army trucks all crowded the sprawling blast area.