MOGADISHU  - Somalia's Al-Qaeda-linked Shebab insurgents shrugged off recent strategic losses and growing military pressure Tuesday to launch an ambush on the president - who escaped unhurt - and fire on foreign warships.

Two Somali soldiers were wounded when Shebab gunmen opened fire as President Sharif Sheikh Ahmed drove down the Afgoye corridor, a key road and home to the world's largest concentration of displaced people, for the first time since its capture on Friday.

"Desperate terrorist militants tried to disturb the visit of the president at the Afgoye corridor by ambushing his convoy, but security forces repulsed them," said Somali security official Mohamed Moalim. "The president is well and continued his trip smoothly." The armoured convoy was guarded by African Union troops and Somali government soldiers, who seized the insurgent stronghold of Afgoye last week after a four-day battle.

The pro-Shebab website said Shebab fighters had carried out the attack against "the head of the enemy" and that Sharif had been saved after he was "surrounded by African Union troops and white gunmen for his safety."

The loss of Afgoye, which controls key roads some 30 kilometres (18 miles) northwest of the capital Mogadishu, was another major blow for the insurgents, who have been on the back foot for several months despite launching a wave of guerrilla attacks.

While the Shebab still control large parts of southern Somalia, regional armies and government troops have been clawing territory off them, with AU forces in Mogadishu, Ethiopian soldiers in the south and west, and Kenyan troops with the AU in the south.

After the fall of Afgoye, the port town of Kismayo is the last major rebel bastion. The Shebab said Tuesday they had engaged in a fierce exchange of gunfire there with foreign warships.

"The mujahideen fighters opened fire and repulsed two military ships that approached the coast of Kismayo, they were coming close to the coast when they were attacked," said Sheikh Hassan Yaqub, a top Shebab official in Kismayo.

"They have sped away from the coastal areas after the shooting and they are not there anymore," Yaqub added. "Those war vessels also returned fire," he said, adding that a boy had been wounded in a neighbourhood close to the shore, but that no other casualties had been reported.

The Shebab did not indicate what country the warships were from, but several foreign navies operate anti-piracy patrols off the coast of the Horn of Africa nation. Kenyan army spokesman Colonel Cyrus Oguna said there were "naval patrols close to Kismayo", but could not confirm if Kenyan vessels were those involved in the clashes.

Abdi Yusuf, another resident of the Islamist-held port, said the Shebab had deployed several fighters along the coast.

"They thought the ships were attacking the city, and so they confronted them with heavy fire," he said.

Nine warships in a European Union naval force are currently deployed off Somalia by France, Germany, Spain, Portugal, Italy and the Netherlands.

Earlier this month the EU naval force launched its first land-based attack in a nighttime raid on the Somali coastline, destroying several small boats that the force said were part of pirate operations.

But the force is deployed to tackle piracy and not to attack the Shebab.

Several other nations, including Russia and China, also provide protection for their vessels as they pass through the busy shipping route through the Gulf of Aden and the Indian Ocean.