His solid record during his brief tenure as prime minister between 2010 and 2011 reinforced an image of a relatively progressive technocrat. After resigning the post, Mohamed moved to Buffalo in upstate New York, where he worked as a state transportation department official and where his family still live.
Critics say Mohamed, who is better known as Farmajo, is inexperienced and worry his fiercely independent views could rile neighbouring countries such as Ethiopia and Kenya. Both nations are major contributors to a regional peacekeeping force in Somalia.
The new president is expected to pick his prime minister – who will be from a different clan to conform to the country’s power-sharing formula.
“Though the election doesn’t solve Somalia’s many challenges, it’s an expression of some political progress … and does highlight a vibrant electoral and political engagement,” said Ahmed Soliman, an east Africa expert at Chatham House.
“What we are seeing is the selection of a president to take the country forward … If you look at the region, and countries such as Sudan, Ethiopia, Djibouti or Eritrea, there is a clear contrast. The election is impressive in the context.”
Many had been worried by the threat of extremist violence.
Al-Shabaab, the Islamic militant movement that has fought for power in Somalia since 2009 and is affiliated with al-Qaida, has been slowly driven out of its key strongholds in a campaign by regional and Somali troops but still launches frequent attacks in Mogadishu and elsewhere.
An attack on a military base 10 miles (15km) south of Mogadishu on Tuesday was repulsed while several mortar bombs in Mogadishu on the same day caused no casualties. On Wednesday morning a faction aligned with Isis stormed a hotel in the semi-autonomous Puntland region, further north up the coast,
The election result prompted celebrations in the streets. Photograph: Farah Abdi Warsameh/AP
The threat from al-Shabaab forced the government and its western backers to scrap a plan to give each adult a vote. Officials decided that the challenge of securing polling stations across the country of 11 million people was insurmountable.
The biggest problem may be allegations of systematic corruption. Rival presidential candidates have accused each other of buying the loyalty of lawmakers, and local anti-corruption campaigners say tens of thousands of dollars have been handed to individuals to secure support in the vote.
“This is probably the most expensive election, per vote, in history,” the Mogadishu-based anti-corruption group Marqaati said in a report released on Tuesday.
Western donors admit the vote was far from perfect, but that it marked a modest step forward from 2012 when just 135 elders picked the lawmakers, who chose the president.
They also point to the levels of female participation: almost a quarter of elected MPs are women.
But Fadumo Dayib, a Somali politician who briefly campaigned as a presidential candidate last year, said female MPs would “not stand a chance” if they tried to assert themselves.
“The vote was given to clan elders who just selected the women to represent their interests. If [the women] do not do what they are told they will be ostracised,” Dayib said.
She did however welcome the election of Farmajo. “History in the making. Lawmakers have clearly chosen the choice of the people. They’ve saved #Somalia!! It is a new dawn, a new beginning!,” she tweeted when the results were announced.
This election, which has been largely paid for by the US and EU states, has provoked fierce interest from rival Middle Eastern powers keen to extend their influence in a strategically important part of the world. The United Arab Emirates, Qatar, Turkey and Saudi Arabia have all been accused of funding the campaigns of specific candidates and thus indirectly fuelling corruption.
A win for the incumbent would not have pleased western powers. British parliamentarians reported last year that “there is a tangible sense amongst western donors that President Mohamud’s government has not been the fresh start that Somalia needed”.
Mohamud, who got 97 votes to the winner’s 184, accepted the result and urged “all Somalis to support the new president and work with him and his government”.
In Nairobi, the capital of Somalia’s powerful neighbour Kenya, members of the Somali diaspora also celebrated. In the neighbourhood of Eastleigh, known as “little Mogadishu” due to its huge Somali community, a group of businessmen invited fellow Somalis to dinner as the election results were announced.
“We are … celebrating. We watched for the whole election process today. We are very happy. I hope I will be able to return to my country because the new president just declared he will bring back peace,” said Khalid Abdullahi, 27, a Somali refugee who lives in Nairobi.