By any measure it is a stunning reversal. Three years ago Somalia's central "government" controlled little more than a square mile of the capital, Mogadishu, while Islamist militants began to impose strict sharia law on most of the country.

Today it is al-Shabaab that has not only been driven out of the capital but looks set to relinquish its last major urban stronghold, the port city of Kismayo.

The dramatic turnaround has been achieved by a display of purpose and co-ordination from the African Union, a body long criticised as lumbering and divided. Its 17,000-strong peacekeeping force, Amisom, led by troops from Uganda, Kenya and Burundi, have turned the tide against the extremists.

Behind the scenes is another quietly satisfied winner: the United States. Al-Shabaab is a self-declared affiliate of al-Qaida and therefore seen as fair game in the so-called war on terror. The US has carried out air strikes against suspected members and provided training and equipment to Amisom. With memories of "Black Hawk Down" in 1993 still raw, it has arguably achieved more than in Afghanistan, without putting a boot on the ground.

The loss of Kismayo would be a potentially terminal blow to al-Shabaab, depriving it of a key source of revenue from charcoal exports and taxes on local traders. In the meantime, Somalia has gained a new president and government, thousands of expatriates are heading home and the country has its best chance of peace for 20 years.

However, cries of mission accomplished would be absurdly premature. First, the Kenyan "liberators" will not necessarily be greeted with sweets and flowers. Their naval bombardment of the port city has reportedly killed civilians, including two teenage boys and a pregnant woman, and caused thousands to flee. This week a Kenyan soldier allegedly went renegade and shot dead six civilians.

Al-Shabaab has reportedly been distributing weapons and may count on local sympathy if it clings on, seeking to test Kenyan soldiers' stomach for battle in prolonged urban warfare. As in Mogadishu, expect more roadside and suicide bombings as the group seeks to score propaganda coups.

It could also melt into hideouts in mountains, towns and villages in the southern countryside, while some members may seek to push across the border into Kenya.

In July 2010, al-Shabaab carried out suicide bombings in the Ugandan capital, Kampala, as revenge for its involvement in Amisom. And with Kenyan troops fighting on the beaches of Kismayo, the next target for retribution could be Nairobi.

Perhaps the biggest threat is the vacuum al-Shabaab will leave behind. While Somalia's president, Hassan Sheikh Mohamud, has more legitimacy than his predecessors, the clan factionalism that has long tormented Somalia for so long has not gone away.

The Kenyan army has been supported by Ras Kamboni, a local militia likely to expect a slice of Kismayo's lucrative cake. Neighbouring Ethiopia and Kenya may also agitate through proxy militias.

"The big danger is Ogaden retaliation against Marehan people," says one regional analyst, who does not wish to be named. "Both are Darod sub-clans but the Marehan [in effect] sided with al-Shabaab's leadership to help oust the warlord Sheikh Ahmed Madobe, leader of Ras Kamboni, from Kismayo. His support is among Ogaden who might feel it is their turn to control the city and want revenge. So there is a lot of potential for inter-clan clashes and also still concern about the conduct of the Kenyan forces following last week's execution of six people."

It would be churlish to deny Somalia's remarkable progress in the past year. But the apparent imminent demise of al-Shabaab is far from the endgame. A generation of conflict is likely to take at least a generation to heal.