Analysts say Ahmed has a real possibility of reuniting Somalis, given his Islamist roots, the backing of parliament and a feeling in once hostile Western nations that he should now be given a chance to try to stabilise the Horn of Africa nation.

"As for the international concerns of piracy and the misinterpretation of Islam we will take concrete action," Ahmed said after being sworn in the same hotel conference hall where a peace deal was signed to bring the opposition into government.

There is widespread recognition both within and outside Somalia that reconciling 10 million people who have been tormented by clan-fuelled violence and anarchy for the past 18 years is a daunting task.

"What lies ahead in a best case scenario is a painfully slow political process aimed at building a coalition of the centre, one local entity or leader at a time," said Somalia expert John Prendergast, co-chairman of the U.S.-based advocacy group the Enough Project.

There were some signs of hope in Somalia's capital Mogadishu after Ahmed was elected in an all-night parliament session held in neighbouring Djibouti due to security concerns at home.

Residents fired anti-aircraft missiles into the sky in celebration after a long vigil in front of the television or next to radios. In the morning, people in Mogadishu waved green branches to show their support and marched in the streets.

A 42-year-old former high school geography teacher, Ahmed headed the sharia courts movement that defeated Mogadishu's powerful warlords and brought some stability to the capital and most of south Somalia in 2006.

While initially welcomed for bringing order, the West accused the Islamic Courts Union of links to extreme terrorist groups and Washington's chief regional ally, Ethiopia, sent troops to drive the Islamists from power.

SUMMIT IN ETHIOPIA

Ahmed fled the country and set up the opposition Alliance for the Re-Liberation of Somalia (ARS) with Islamist allies, while insurgents in the country began fighting to remove the Ethiopian troops, who finally pulled out this month.

"It reduces the Ethiopian footprint to a fingerprint and puts the Somalis back in control," said a Western diplomat, adding that Islamist fighters should also give Ahmed a chance.

Al Shabaab, which is on Washington's list of foreign terrorist groups, said just before the vote that it would start a new campaign of hit-and-run attacks on the government -- whoever came to power.

"Sheikh Sharif and the election in Djibouti is not something to be supported," Sheikh Hassan Yacqub, al Shabaab spokesman in the southern city of Kismayu, told Reuters on Saturday.

Some regional leaders and Western diplomats say al Shabaab is made up of little more than clan-based bandits using the banner of religion to justify their crimes.

In the past two years, more than 17,400 civilians have died during the insurgency, a third of the population now relies on food aid and some 2.5 million have been driven from their homes, a Somali human rights group said on Saturday.

The Islamist leader will fly on Sunday to the very country that booted him from office, as a president attending an African Union summit in Ethiopia's capital Addis Ababa.

He will return to Mogadishu after the summit and his immediate task is to try to put together a unity government -- the 15th such attempt since Somalia descended into anarchy with the overthrow of the dictator Mohamed Siad Barre in 1991.

Ahmed said his government would not tolerate any abuse of power or corruption and treat neighbours with respect.

Legislators hope they have elected a man able to isolate or even bring on board hardline insurgents, but warned his task would be even tougher unless he forms a true unity government.

(Additional reporting by Abdi Sheikh and Abdi Guled in Mogadishu and Abdiaziz Hassan in Djibouti; Editing by Katie Nguyen)