The announcement came late on Sunday evening after the country's MPs gathered during the day for a vote at Mogadishu's airport area.
It was when the incumbent President Mohamed Abdullahi Mohamed's term of office expired in February 2021 that Somalia was put into a political crisis.
No successor had been appointed, partly due to the deadly violence going on in the country, but also due to a power struggle between the president and Prime Minister Mohamed Hussein Roble, who is also a Swedish citizen.
The president's reluctance to resign led, among other things, to street fighting in Mogadishu.
Pressure from the outside world
President Mohamed, known as Farmajo, had hoped to win another term, but in the end - after three rounds of voting on Sunday - the choice fell on Hassan Sheikh Mohamud.
He is also a former Somali president and held the post between 2012 and 2017.
The outside world has been pushing for the presidential election because it is worried that other difficult problems to deal with - such as the fight against the jihadists in the extremist movement al-Shabaab - will otherwise be delayed.