Lawmakers on Monday chose Hassan Sheikh Mohamud as Somalia's new president after the 56-year-old lecturer got the majority of votes in a second round run-off, beating outgoing president Sharif Sheikh Ahmed.
Sharif had earlier in the day sounded confident of victory in the vote, which UN brokers have billed as a major milestone in stabilising the war-torn nation.
Many sceptical observers had voiced fears that the vote would simply return the same corruption-tainted leadership but dark horse Mohamud, an academic and activist, won 190 votes against 79 for the outgoing president, who had been seen as the likely winner until late in the day.
Mohamud came second out of 25 candidates in the first round, clocking up 60 votes, against 64 for Sharif. His victory in the run-off was greeted with joyful firing into the air in the streets of Mogadishu.
Mohamud, who, like Sharif, comes from the Hawiye clan, is linked to the Al-Islah party, the Somali equivalent of the Muslim Brotherhood. He is the founder of a university in Mogadishu and has worked alongside several international organisations in Somalia.
"What has happened today will be written on a golden page in Somalia's history and my friend Sheikh Sharif will always be credited for his role in this development," the new president said.
"I hope that Somalia will from now on start heading towards better days and that all problems we have undergone will be history," he said.
Sharif conceded defeat in the poll, which is the final stage of a UN-backed process to set up a new administration for the war-torn Horn of Africa country.
"I congratulate my brother Hassan Mohamud for his victory which is fair and I'm very much pleased with it," Sharif said in a speech after the results were announced.
"I'm ready to work with him as I would have liked people to work with me had I won the election. I will step aside without a grudge because I'm very happy that my country is no longer in transition," Sharif said.
The new parliamentarians, selected last month by a group of traditional elders, rose one after the other amid tight security to cast their ballots in a packed room at the police academy.
African Union and UN security officials conducted body searches and fingerprint checks on all those allowed anywhere near the venue. Islamist extremists have often taken advantage of gatherings attended by senior political figures to launch suicide attacks.
Each candidate had to pay $10,000 (7,850 euros) to enter the race, which has been delayed several times, missing an August 20 deadline.
The UN's special representative for Somalia, Augustine Mahiga, said Monday that while obstacles remain, the presidential election will "mark another milestone in the country's political process".
Analysts had been gloomy on the process, suggesting it offered little but a reshuffling of key figures and positions from the outgoing Western-backed government.
Somalia has lacked an effective central government since president Mohamed Siad Barre was ousted in 1991, unleashing cycles of bloody conflict that have defied countless peace initiatives.
Ruthless warlords and militia groups including Al-Qaeda-linked Shebab insurgents have controlled mini-fiefdoms that African Union troops and other forces have only recently started to capture.
Sharif, in power since 2009, had been one of the favourites, although he cut a controversial figure with Western observers.
A UN report in July said that under his presidency, "systematic embezzlement, pure and simple misappropriation of funds and theft of public money have become government systems" -- claims Sharif has rejected.
The rival challengers had bitter arguments, divided along Somalia's notoriously fractious clan lines, and the UN Security Council has repeatedly warned of "intimidation and corruption", threatening "action against individuals whose acts threaten the peace, stability or security of Somalia".
However, Britain's ambassador to Somalia, Matt Baugh, said last week that both the outgoing president and prime minister had assured him of "their commitment to respect (the) election outcome".