The vote and swearing-in took place in Djibouti because the international community wanted to be present to observe the voting process and could not do so in Somalia for safety reasons.

The Djibouti location had been chosen before radical Islamic fighters took over the Somali parliament building and the presidential palace this week.

Sheikh Sharif won a comfortable majority of the votes in a second round of voting early Saturday after one of his main rivals, Nur Hassan Hussein Nur Adde, the former prime minister of Somalia, withdrew from the race.

Nearly 300 members of the nearly 500-member Somali parliament voted in favor of Sheikh Sharif.

The new president was a founding member of the Union of Islamic Courts, which Ethiopia ousted from Somalia in December 2006 after the the group took over Mogadishu that year.

Don't Miss

Ethiopia's invasion had the blessing of the United States, which accused the Union of Islamic Courts of harboring al Qaeda fugitives. But various Islamist groups -- including the hard-line Al-Shabab, which the United States has designated a terror organization -- rejected the presence of Ethiopian forces and mounted an insurgent campaign against the Ethiopians and the transitional government.

Sharif's faction of the Union of Islamic Courts is known to be moderate and cooperative in peace accords.

Osman said Sheikh Sharif's appeal largely lies in the perception that he is able to challenge the ideology of Al-Shabab.

In a speech Saturday after his win, the president declared that he will work for peace.

"I will use all my power to bring back peace to Somalia," he told cheering members of parliament.

He also vowed that Somalia will be a friendly nation to its neighbors.

Soon after his win was announced, celebratory gunfire erupted in the capital city of Mogadishu.

On Monday, fighters from Al-Shabab seized control of the seat of Somalia's U.N.-backed transitional government, raiding the parliament building and capturing five parliament members. The fighters also took over the presidential palace in Baidoa, in the southwestern part of the country.

The members of parliament were paraded through the city streets with hundreds of residents looking on, according to a journalist who witnessed the public spectacle.

The raids came a day after Ethiopia announced that all its forces had left Somalia.

Last week, as Ethiopian troops began pulling out of the Somali capital, forces from Islamist groups including Al-Shabab took control of bases the Ethiopians abandoned around Mogadishu.

With Monday's takeover of Baidoa, the transitional government has control of only the presidential palace in Mogadishu and the road to the airport there, which it holds with the help of African Union forces.

The transitional government has maintained very little control over the country, even with the support of the Ethiopian forces. It has also been wracked by a power struggle between Prime Minister Nur Hassan Hussein and President Abdullahi Yusuf Ahmed, who resigned in December.

As parliament members applauded the new president Saturday, it was unclear when they would be able to return to their country.