Somali MPs meeting in Mogadishu have elected Hassan Sheikh Mohamud as the country's new president, in the latest step to end decades of war.
The academic beat President Sheikh Sharif Sheikh Ahmed in a run-off poll by 190 to 79 votes, officials said.
No candidate secured the required two-thirds majority in the first round of voting, conducted by secret ballot.
It is the first time for years that a president has been chosen on Somali soil, a sign of improving security.
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.
Despite qualifying for the second round, outgoing Prime Minister Abdiweli Mohamed Ali and moderate Islamist Abdulkadir Osoble then pulled out after coming third and fourth respectively. Eighteen candidates were eliminated at the first hurdle.
Outgoing Somali President Sheikh Sharif Sheikh Ahmed conceded defeat during a live broadcast on national TV, saying he was "satisfied" with his time in power.
"It is a great pleasure for me to witness a fair election in Mogadishu after 42 years [since Mohamed Siad Barre took over in 1969]," he said.
The new president was sworn into office immediately after the result.
Hassan Sheikh Mohamud, 56, is an academic as well as a civic activist who has worked for several national and international peace and development organisations.
He graduated from the Somali National University in 1981 and went on to study in India, where he obtained a master's degree from Bhopal University.
For two years he worked for the United Nations children's organisation Unicef as an education officer in south and central Somalia, until the departure of UN peacekeepers in Somalia in 1995.
Four years after that he co-founded the Somali Institute of Management and Administration Development in Mogadishu, which later evolved into Simad University.
In 2011, he founded the Peace and Development Party and is currently serving as its chairman. He speaks Somali and English and is from the Hawiye clan - one of Somalia's biggest.
The election process began five hours late at a police academy in Mogadishu, following tight security checks.
The election was also delayed by the swearing-in of the last batch of MPs and then a vote on whether a group of disputed MPs, including former warlords, could take part. The MPs voted in favour of this.
The new speaker of parliament, Mohamed Osman Jawari, had urged MPs to vote with their consciences.
"May God help us to elect a good leader in an atmosphere of tranquillity. We must give the youth of Somalia a bright future," he said.
The process is still in many ways owned by outside powers who have for years been involved militarily and politically in Somalia, the BBC's Mary Harper reports.
But Hassan Sheikh Mohamud could represent a different kind of future for the country because he is not associated with the violence and corruption of the past, our correspondent says.
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 the al-Shabab militia.
Since the overthrow of President Siad Barre in 1991, Somalia has seen clan-based warlords, Islamist militants and its neighbours all battling for control.