But in the past few months, the Shabab has taken a beating in steady urban fighting against a better-armed, 9,000-strong African Union peacekeeping force. Many analysts have said the Shabab was growing weaker by the day, riven by infighting and nearly out of cash.

The rebels have also been divided over whether to let in Western aid organizations to relieve the famine. As the famine’s toll continues to mount, pressure has built on Shabab leaders, especially those with connections to local clans.

The Shabab has also blocked starving people from leaving its territory and has even set up its own large displaced persons camp about 25 miles from Mogadishu where families trying to escape Shabab territory are essentially imprisoned. Those who have escaped have painted a bleak picture of entire villages emptied out by famine.

The rebels’ departure from the capital offers no guarantee that Somalia’s weak transitional government, which has let innumerable other opportunities slip through its fingers, will be able to gain control of Mogadishu, or that the city’s population will rally behind the government. The Transitional Federal Government has been propped up by millions of dollars of Western aid, including American military aid, but its leaders remain ineffectual, divided and by many accounts corrupt.

“Unfortunately, I entertain no delusions that the T.F.G. will rise to the occasion,” said J. Peter Pham, Africa director at the Atlantic Council, a Washington-based policy institute. “The only exploiting that will be taking place will be whatever schemes its ministers will hatch to profit from increased aid flows.”

As the nation faces one of the worst droughts in 60 years, many analysts are pessimistic that the government will be able to meet even the needs of the people in the capital. On Friday, government troops looted sacks of grain and killed several people during a riot over emergency food in a refugee camp.

Image Rebel forces still control much of southern Somalia. Credit... The New York Times

Not since 2007 has the government had such an opportunity to assert itself in Mogadishu. In late 2006, Ethiopian troops stormed into Somalia and pushed out a grass-roots Islamist movement that was ruling much of the country, and for a brief spell the transitional government was in control. But within months the Shabab was waging hit-and-run attacks, and by 2008, it had seized several towns across the country and neighborhoods in Mogadishu.