Somalia's former leader Hassan Sheikh Mohamud has been elected president after a final vote that was only open to the country's MPs.
He defeated the current president, Mohamed Abudallahi Farmajo, who has been in office since 2017.
The ballot was limited to Somalia's 328 MPs due to security concerns over holding a wider election, and one of them did not cast a vote.
Mr Mohamud received 214 votes, defeating Mr Farmajo who won 110 votes.
Three MPs are reported to have spoiled their ballots.
The unusual circumstances highlight Somalia's security issues as well as the lack of democratic accountability.
The result marks a comeback for Hassan Sheikh Mohamud, who served as Somalia's president between 2012 and 2017 before he was defeated by Mr Farmajo.
The elections - which were hotly contested and went to a third-round - were delayed for almost 15 months due to infighting and security issues.
Mr Mohamud was sworn in shortly after the final results were announced, prompting supporters in the capital to cheer and fire guns into the air. He will serve for the next four years.
In the vote on Sunday, hundreds of parliamentarians cast their ballots at a fortified aircraft hangar in the capital Mogadishu.
Explosions could be heard nearby as voting was taking place, but police said no casualties were reported.
As the incoming president, Mr Mohamud will have to deal with the impact of an ongoing drought in which the UN says 3.5 million Somalis are at risk of severe famine.
But the big task he faces is to wrest control of much of Somalia from al-Shabab. The al-Qaeda-linked Islamist militant group continues to dominate large parts of the country and carries out frequent attacks in Mogadishu and elsewhere.
The country is also being affected by food and fuel inflation sparked by the war in Ukraine.
The government is backed in its fight against al-Shabab by the African Union, in the shape of some 18,000 troops and the United Nations.
The instability is one of the reasons why Somalia has been unable to hold direct elections. Somalia has not had a one-person-one-vote democratic election since 1969.
That vote was followed by a coup, dictatorship and conflict involving clan militias and Islamist extremists.
This is only the third time that the indirect election for president has been able to take place in Somalia itself. Previous ones were held in neighbouring Kenya and Djibouti.
How did the voting work?
This vote was supposed to have happened last year when Mr Farmajo's four-year term ended. But political differences and instability delayed the poll and the president remained in power.
The MPs who chose the new president were themselves elected by delegates nominated by the country's powerful clans.
They gathered in a large airport hangar in the well-guarded Halane Camp. This is the main military base of the AU's mission in Somalia (Atmis), as well as the home of
diplomatic missions and aid agencies.
The voting, done by secret ballot, was delayed for hours due to lengthy security checks.
Past elections were marred by allegations of vote-buying with candidates reportedly offering money in exchange for support.
The only female candidate, former Foreign Minister Fawzia Yusuf Adam, was eliminated in the first round of voting.
What has al-Shabab said?
In previous elections, al-Shabab threatened and even kidnapped clan elders after condemning them for participating in what it saw as an un-Islamic poll.
This time around, its response to the elections has been more muted, with fears that its members or sympathisers may have secretly sought parliamentary seats in a bid to undermine the system from within.
The fear was publicly expressed by neighbouring Djibouti's President Omar Guelleh in 2020, when he was quoted as saying: "I fear we will end up with a parliament indirectly controlled by al-Shabab because they'll have bought the support of some of the MPs."
Some analysts felt Mr Guelleh was exaggerating the possibility of al-Shabab gaining a foothold in parliament, but there is no doubt that it is a major political force in Somalia.
Latest Stories
-
GPL 2024/2025: Prince Owusu screamer earns Medeama win over Young Apsotles
4 mins -
Burning old TVs to survive in Ghana: The toxic trade in e-waste
24 mins -
Perfume boss admitted he ignored Russia sanctions
38 mins -
Wicked proves popular as opening set to be biggest for Broadway film
44 mins -
Nominee for agriculture secretary completes Trump cabinet
56 mins -
ECG urges prepaid customers to top up to last one month ahead of system upgrade
1 hour -
Three more tourists named in Laos methanol deaths
2 hours -
Betway Africa offers a once-in-a-lifetime ‘Play-on-the-Pitch’ experience at Emirates Stadium
2 hours -
The rise and fall of Matt Gaetz in 8 wild days
2 hours -
School Feeding Programme: Bono East NIB seizes smuggled rice, arrest driver
2 hours -
Dr. Razak Opoku: Despite challenges, facts and data still prove NPP is better manager of the economy than NDC
2 hours -
Seidu Agongo: On December 7, it must be peace over power
2 hours -
2024 Election will take place on December 7; ignore misleading claims – EC
2 hours -
GPL 2024/25: Asante Kotoko bounce back with win over Aduana FC
3 hours -
GPL 2024/25: Gold Stars beat Vision FC to retain top spot
3 hours