Mr. Mohamud, chairman of the Peace and Development Party, came in second out of 22 candidates in the first round of voting and then defeated Sheik Sharif Sheik Ahmed, who is the president of the departing transitional federal government, 190 to 79, in a head-to-head runoff.

Mogadishu, once a crumbling, war-torn capital, has shown tentative signs of bouncing back, with reconstructed hospitals, shops and homes. The Shabab, a militant group, has withdrawn from the city but continues to hold onto the port town of Kismayo.

Image Credit... The New York Times

Mr. Mohamud, who is from the town of Jalalaqsi in central Somalia, is a political neophyte but has long worked as a community activist for various nongovernmental organizations in Somalia, including the Center of Research and Dialogue, the International Peace Building Alliance and Unicef. He is expected to name a prime minister, who will form a council of ministers to begin running the long-broken nation.

Augustine P. Mahiga, a Tanzanian diplomat who has been the United Nations special Somalia representative of the secretary general for the past two years, said in a blog post on the eve of the voting that the new leader’s election would mark “one of the most important dates in the history of Somali politics.” He expressed high confidence in the legislative arm of Somalia’s government, saying its members were “capable of delivering a new Somalia in the next four years.”