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/25: Asante Kotoko bounce back with win over Aduana FC
2 mins -
GPL 2024/25: Gold Stars beat Vision FC to retain top spot
39 mins -
Ebo Whyte returns with ‘The 4Play’
1 hour -
2024/25 Ghana League: Heart of Lions sink Legon Cities to go third
2 hours -
Bright Simons: DBG, Ghana’s top development bank, goes for the jugular
2 hours -
Governance and Entrepreneurship consultant demands global support for Africa’s young farmers
2 hours -
Ghanaians reminded to prioritise regular health check-ups
2 hours -
Salah brace sends Liverpool 8 points clear
2 hours -
Leicester City sack manager Steve Cooper
2 hours -
Akwasi Sarpong wins AIBs 2024 Award for BBC OS coverage of Israeli hostage release
3 hours -
Gospel musician Adeline Baidoo shares inspiring story of triumph over adversity
3 hours -
Kwesi Yankah: Escape from Ghana
3 hours -
Musician DeThompson DDT drops new single Happiness
3 hours -
Ukraine’s Grain Initiative raises over $200m, provides lifeline amid global food crisis
4 hours -
Dancehall queen Spice donates to students of 3 basic schools in Accra through MYO Global Foundation
4 hours