A UN-recognised government that controls only three districts of the capital, propped up by international peacekeepers. A raging Islamic insurgency that has rallied support by denouncing its opponents as foreign stooges. Rebel strongholds where medieval justice is meted out and teenage boys have hands and feet chopped off, while women and girls are stoned to death in public. Suicide bombings that kill dozens. Kidnappings and targeted killings that terrorise aid workers and journalists.

The parallels between Afghanistan and Somalia are not hard to find, but should not be exaggerated. Somalia, for one thing, is now the worst humanitarian crisis in the world with half the population in need of assistance and 1.5 million people displaced. Nonetheless, the Horn of Africa nation invites comparisons as a haven for global jihadists where the Islamic militias are fighting to create an "al-Qa'ida state".

This was a country that was abandoned by much of the world after the fall of the socialist dictator Siad Barre in 1991. The interventionist Clinton administration had its fingers burned two years later when it lost 18 servicemen in the incident remembered in the film Black Hawk Down. The UN pulled out two years after that, when the country became a model "failed state" as the country's patchwork of competing clans tore itself apart. It was only after the 11 September attacks on the US that Somalia began to reassume strategic importance for Washington.

Download the new Independent Premium app Sharing the full story, not just the headlines

It was after 2005, when a section of the business community had tired of paying levies to the warlords and started backing a loose alliance of Islamic Sharia law courts, that the dynamic changed. The Islamic Courts Union (ICU) that emerged drew mass support and within a year had driven the warlords from the capital. But diplomatically it was the wrong time for a new Islamic state to emerge, and Somalia was accused of channelling money to al-Qa'ida and sheltering terrorists.

A year later, the US engineered an invasion of Somalia by its traditional enemy Ethiopia and the ICU was scattered by a larger conventional army. The Ethiopian occupation turned out to be disastrously worse. The youth wing of the ICU, al-Shabaab, transformed itself into a fundamentalist militia (admired by al-Qa'ida) that drew deep public support by fighting the occupier. And at the end of last year the Ethiopians retreated.

The international community turned to Sheikh Sharif Sheikh Ahmed, the leader of the ICU, to take over the government – the same man the US helped to topple two years prior. He promptly installed sharia, but found that he could no longer control the al-Shabaab or his former ally Sheikh Aweys who now heads another Islamic militia, Hizbul-Islam.

The government has lost much support since taking over, and militants now control most of south and central Somalia. Open backing from Washington has bolstered support for Sheikh Ahmed's opponents. The question now worrying Western security experts is whether Somalia will become a training ground for jihadis from Somalia's huge diaspora. Certainly al-Shabaab is recruiting in the US and Europe and suicide bombers in Somalia have come from communities as far afield as Minnesota. An alleged terror plot in Australia earlier this year involving Somalis has rung alarm bells.

However, Chatham House's Somalia expert, Roger Middleton, cautions against premature conclusions about Somalia becoming "the new Afghanistan". "Shabaab and Hizbul-Islam are nationalist movements first and foremost. The commanders are fighting to control Somalia. Their agenda is local, not global." There is a danger, he says, that the Afghan template being pushed down on Somalia "becomes a self-fulfilling prophecy".