According to witnesses, around 10:45 a.m., a 15-ton truck loaded with fuel drums pulled up to the entrance of the compound, which houses various government ministries, including those dealing with education, foreign affairs and labor issues. Another fuel truck was passing by, and Ali Mohamed, a bystander, said the driver of the truck filled with explosives chose that moment to detonate them.

“But we are lucky; the oil tanker passing by did not explode,” Mr. Ali said.

Still, he said, “This is the most horrific incident I have ever witnessed.”

The explosion sent a gigantic fireball into the sky and shattered windows for miles around. It spewed bodies across an area several city blocks wide. Many were small and thin and appeared to be children, charred beyond recognition. Somali government officials said no senior officials were hurt.

The Shabab introduced suicide bombs to Somalia, and since Shabab fighters began their insurgency in 2007, they have struck many times, with victims including Somali lawmakers, African Union peacekeepers and poor women sweeping up Mogadishu’s bullet-pocked streets.

But few, if any, of their attacks have killed as many people as the one on Tuesday.

There had been hopes that the Shabab’s withdrawal from Mogadishu would usher in a new era of stability, at least in the capital. For the first time in years, the transitional government, backed by 9,000 African Union peacekeepers, was nominally in control. Certain areas, including the area near the stricken government compound, were considered relatively safe, and in recent months, traders had returned to streets pulsing with more life than there had been for a long time.