The blast hit the lower floors of the downtown tower block, throwing debris into the streets and sending workers running outside.
Interior Minister Miguel Angel Osorio Chong said the blast killed at least 25 people and injured 100. It was not yet clear what caused the explosion, and the death toll could still rise, he added.
Media reports said machinery inside the building had apparently exploded. But an ambulance service official at the scene, who asked not to be named, said it was caused by a gas leak.
Police quickly cordoned off the building, and television images showed the explosion caused serious damage to the ground floor and blew out windows on the lower floors of the tower.
"The place shook, we lost power and suddenly there was debris everywhere. Colleagues were helping us out of the building," witness Cristian Obele told Mexican television.
Some people at the scene said the blast came from a neighboring building.
Pemex said initially its headquarters had been evacuated because of a problem with its electricity supply. It then said there had been an explosion, but did not say what caused it.
Helicopters buzzed around the building and lines of fire trucks sped to the entrance, while emergency workers ferried injured people through wreckage strewn on the street.
"Now we are in rescue mode and looking for people and for bodies," Osorio Chong said.
Search-and-rescue dogs were sent into the skyscraper, a Mexico City landmark more than 50 floors high and sporting a distinctive "hat" on top.
Gloria Garcia, 53, a Pemex worker not in the building during the explosion, came to see if she could track down her son, who works in one of the floors hit.
"I'm calling his phone and he's not answering," Garcia said, weeping as she called repeatedly on her phone. "Nobody knows anything. They won't let me through. I want to see my son whatever state he's in."
Plaster fell from the ceiling of the basement, and the situation at the Pemex tower was dangerous, a spokesman for local emergency services said.
Pemex has experienced a number of deadly accidents in recent years. In September, 30 people died after an explosion at a Pemex natural gas facility in northern Mexico.
More than 300 were killed when a Pemex natural gas plant on the outskirts of Mexico City exploded in 1984. Eight years later, about 200 people were killed and 1,500 injured after a series of underground gas explosions in Guadalajara, Mexico's second biggest city.
(Additional reporting by Gabriel Stargardter and Liz Diaz; Editing by Kieran Murray and Peter Cooney)