Hassan Sheikh Mohamud is Somalia's new president, according to official results from Mogadishu.
The former opposition leader had been considered to be a "dark horse" candidate, reported Al Jazeera's Peter Greste.
Mohamud had come second to the incumbent president, Sheikh Sharif Ahmed, in the first round of voting among Somalia's parliamentarians.
But he then passed the 50 per cent threshold in the second round, winning by 190 votes to 79.
"People here have told me that they all want change," said Al Jazeera's Nazanine Moshiri, reporting from Mogadishu. "People want a new president, and it seems that is what they have got.
"Part of what happened today came after some alliances were formed between Hasan Sheikh and Prime Minister Abdiweli Ali."
The prime minister had been vying to become head of state, but came third in the first round of polling among the country's MPs, and conceded defeat. It appears his supporters threw their weight behind the opposition candidate, pushing him over the electoral threshold.
There was an hour between the results of the first round being announced and the beginning of voting in the second, during which much political horse-trading reportedly took place.
"I spoke to Hassan Sheikh on Sunday," said Nazanine Moshiri. "He is a moderate, whereas the current president is viewed as an 'Islamist'. He is an academic and might be able to unite the various clans behind him.
"One of the problems we have here in Mogadishu is the rise of armed militia groups, and the country needs someone who they can unite behind, as there is a worry that there could be fighting between these armed militias."
|Peter Greste analyses what the
result will mean for Somalia
Bribery mars poll
Al Jazeera has learnt that the electoral campaign was marred by allegations of bribery, with payments of up to $50,000 said to have changed hands between Somalia's lawmakers.
Senior diplomatic sources deployed in the region to ensure an honest and fair election, have confirmed that votes had been bought and sold over the past few days.
It was hoped that the presidential vote, the first of its kind in decades, would alter the political landscape of the nation and be a milestone in the war-ravaged country's quest to end two decades of violence, corruption and infighting.
However, the bribery allegations confirm fears of the election being rigged.
Earlier, international diplomatic sources told Al Jazeera that "if MPs don't vote with their conscience, this will continue the orgy of corruption".
Al Jazeera's Nazanine Moshiri reported: "I have spoken to a senior diplomatic source as well as various MPs and people connected to the technical selection committee as well.
"They have told me that there is bribery going on all sides. Many of the main candidates who are taking part in this election have been offering money to MPs, and some of those MPs have accepted that money.
|Who is Hassan Sheikh Mohamud?
- Born in 1955 in Jalalaqsi on the
Shebelle River in central Somalia
- Studied in the United States and India
- Became a university lecturer
- Worked for the World Bank and the
United Nations Development Programme
- Worked in Somalia civil society since 2008
- Published a book on the history of
mediation in Somalia in 2009
- Founded the Peace and Development
Party in 2011, and became its chairman
"We are talking about large amounts of money, ranging from what I have been told is $10,000 to $50,000. Some MPs are even being asked to swear on the Quran to vote for a certain candidate.
"One senior politician told me that he believes a quarter of parliament will vote honestly and fairly - but unfortunately the rest will not."
In addition, a new group of MPs were sworn in "at the last minute", our correspondent said. These new parliamentarians had previously been excluded over allegations of being connected to warlords.
Some presidential contenders and Somalis had criticised the election process, suggesting that it would merely bring in a new government that would look much like previous ones.
A diplomatic source in Mogadishu said millions of dollars were being used by all main candidates to bribe lawmakers to influence their vote.
"Seven million dollars is estimated to have come from Gulf sources, and the money is intended to ensure that President Sharif is re-elected," said the source, who declined to be named, due to the sensitivity of the matter.
The source said the money was coming from Somali business interests in Gulf Arab countries, some of whom have connections to warlords and want to maintain the status quo.
|Al Jazeera's Peter Greste and Nazanine Moshiri
report from Mogadishu
The office of the president has denied any suggestion of wrongdoing, and called the allegations "baseless and not true" adding that "President Sheikh Sharif's campaign has run out of money".
Earlier on Monday, Afyane Emli, professor of international affairs at Qatar University, told Al Jazeera the election was based on an arbitrary procedure, where traditional elders and clan members selected parliamentarians.
"Somali people do not elect the MPs that are electing the so-called president. So, technically we have an arbitrary and corrupt process that has produced the MPs now and there are so many things that are far from transparency and openness," he said.
"The three incumbents that are in power have manipulated the process to their advantage. The front runners, the president, the prime minister and the former speaker of the parliament have had significant impact on how the process develops for quite some time".
There has been no effective central government control over most of the country since the outbreak of civil war in 1991 and Monday's vote is seen as a culmination of a regionally brokered and UN-backed roadmap to end that conflict, during which tens of thousands were killed and many more fled.
The vote is the first to take place in Somalia in decades and has been made possible by African Union, Kenyan and Ethiopian troops who have pushed al-Qaeda-linked fighters out of more and more areas.