Moderate Islamist Sheikh Sharif Sheikh Ahmed was sworn in as Somalia's new president on Saturday just hours after his election during an all-night parliamentary session held in neighbouring Djibouti, dpa reported.
Observers said they believe Sheikh Sharif is the best candidate to unite warring Islamic factions in the Horn of Africa country, which has been plagued by chaos since the 1991 ouster of former dictator Mohamed Siad Barre.
Sheikh Sharif, 44, took the oath of office in time to fly to the Ethiopian capital, Addis Ababa, for an African Union summit.
Local media reported that many Somalis stayed up all night to hear the result, and when it became clear that Sheikh Sharif would win, fired celebratory shots in the air in Mogadishu.
Sheikh Sharif received 293 votes out of 420 ballots cast with Barre's son Maslah Mohamed Siad receiving 126 votes.
Prime Minister Nur Hassan Hussein, one of the early favourites, withdrew after receiving only 59 votes in the first round.
Sheikh Sharif is the head of the moderate branch of the Alliance for the Re-Liberation of Somalia (ARS) and headed up the Islamic Courts' Union (ICU) when it controlled Mogadishu for six months in 2006.
The ICU installed strict Islamic law and helped bring stability to Somalia during its reign, tackling warlords and curbing the piracy that once again exploded after it was ousted.
Ethiopian forces invaded in late 2006 to help kick out the ICU, sparking a bloody insurgency that has killed an estimated 16,000 civilians and displaced around 1 million.
The election came after President Abdullahi Yusuf Ahmed, a former warlord, resigned in December when parliament thwarted his attempt to sack Hussein. He failed to bring peace to Somalia during his four years in charge.
Parliament was enlarged to 550 MPs Monday to accommodate 200 members of the ARS as part of a UN-backed peace process to create a unity government and end the instability.
Ahmedou Ould-Abdallah, the UN's special envoy for Somalia, praised the vote.
"We are finally seeing progress from the hard work by all sides to create an inclusive parliament," Ould-Abdallah said in a statement.
The new government and president now face a major challenge in governing.
The main insurgent group, al-Shabaab, a militant offshoot of the ICU, controls much of Somalia, including the seat of parliament in Baidoa.
Al-Shabaab, which has been waging a bloody insurgency since early 2007, seized Baidoa on Monday, hours after the Ethiopian troops who had been propping up the central government for two years left the country.
The group has vowed to continue fighting to ensure that strict Islamic law is imposed in all of Somalia.
Sharif, speaking to lawmakers after being elected, said he would serve with no regard to colour or clan to try to finally bring some stability to Somalia.
"I call on other Somalis who are not part of this peace process to join us," he said.
He also promised to work closely with regional governments and to tackle the humanitarian crisis gripping his country.
The insurgency, combined with drought and rising food prices, has created a humanitarian catastrophe. About 3.25 million people in Somalia, almost half the population, are dependent on food aid.
With the departure of Ethiopia, only an undermanned African Union force of around 3,000 troops from Uganda and Burundi remained to back government forces although the two countries have put two extra battalions on standby.
The AU is trying to scrape up more troops, but the United Nations has ruled out sending in a peacekeeping force.
However, al-Shabaab faces opposition from the government-aligned Islamist group Ahlu Sunnah Waljamaca, which recently seized control of two towns from al-Shabaab.