However, Islamist militants said the election was organised by the “enemies of Somalia”.
Somalia has suffered 20 years of war.
Accepting his defeat, former President Ahmed said it was the first fair election in Mogadishu for 42 years – since Mohamed Siad Barre took over in 1969.
‘Nefarious geopolitical agenda’
President Mohamud, a moderate Islamist academic, was sworn in immediately after it was announced that he had beaten Mr Ahmed by 190 to 79 votes in a ballot of newly elected MPs.
Who is Hassan Sheikh Mohamud?
- Born in central Hiran region in 1955
- From the major Hawiye clan
- Married, speaks Somali and English
- Linked to al-Islah, Somalia’s branch of the Muslim Brotherhood
- Studied engineering at the Somali National University and became a lecturer in 1981
- Five years later went to India to study, obtaining an MBA from Bhopal University
- Stayed in Somalia during the civil war, working as a consultant with non-governmental groups, UN bodies and on several peace initiatives
- Helped set up the Simad University in 1999, and was its dean for 10 years
- Founded his Peace and Development Party (PDP) in 2011
- Elected an MP in August 2012
“I promise that Somalia reclaims its place in the world community as of today – and to do that, we must ensure that we move forward,” he said in his victory speech.
Both the British prime minister and UN envoy to Somalia Augustine Mahiga warned there was still much to do.
“Somalia’s leaders must now work together to build a more representative and transparent system, tackle corruption and strengthen security and stability,” said Mr Cameron, who hosted a major conference on trying to bring peace to Somalia in February.
Mr Mahiga said the Somali people had “taken a great step forward on the path to peace and prosperity”.
EU foreign policy chief Catherine Ashton has called up Somalia’s new leader to congratulate him, according to the AFP news agency.
However, the al-Qaeda-linked group al-Shabab still controls many southern and central parts of the country, and has staged frequent suicide attacks in the capital since it was driven out of Mogadishu last year by African Union troops and pro-government forces.
“We will only recognise a process run by Somalis and not manipulated by Ethiopia, Burundi, Kenya, Uganda, Djibouti and the West,” al-Shabab spokesman Sheikh Ali Mohamud Rage told the AFP news agency.
“Hassan is operating under a constitution drafted by the enemies of Somalia, who have a nefarious geopolitical agenda, especially neighbouring countries.”
Hassan Sheikh Mohamud, 56, is a civic activist who founded a university in Mogadishu and has worked for several national and international peace and development organisations.
He has links to al-Islah, Somalia’s branch of the Muslim Brotherhood, which is strongly opposed to al-Shabab.
He stayed in Mogadishu throughout the years of conflict, unlike many intellectuals, and he has previously held a post in government.
Like former President Ahmed, he is from the Hawiye clan – one of Somalia’s biggest.
BBC Somali analyst Mary Harper says President Mohamud could represent a different kind of future for the country because he is not associated with the violence and corruption of the past.
Nevertheless he faces massive challenges on multiple fronts, she adds. Firstly, he will have to deal with the powerful politicians who lost the elections; then he has to try to reunite a country torn apart by two decades of civil conflict, much of which is controlled by al-Shabab.
Since the overthrow of President Siad Barre in 1991, Somalia has seen clan-based warlords, Islamist militants and its neighbours all battling for control.
Are you in Somalia or the wider region? What is your reaction to the election of Hassan Sheikh Mohamud? You can send us your comments using the form below.