Five inmates said to be from Somalia's Al-Shabaab jihadist group and a guard were killed in a blaze of gunfire during an attempted breakout from the main prison in the capital Mogadishu on Saturday, a prison official and witnesses said.
Jailed members of the Al-Qaeda-linked group, which has been waging a bloody insurgency for years, managed to obtain weapons and launched what appeared to be a well-planned bid to escape the prison, security sources said.
Witnesses living near the prison compound reported hearing the sound of grenade explosions and machinegun fire.
"All five inmate assailants who tried violently to escape the prison were killed and neutralised after engaging in an armed confrontation with the prison guard force," Mohamed Hassan, an officer with the Somali Custodial Corps, told AFP.
"A member of the prison guard force died and three others were wounded in the incident," he added.
"The security agencies are investigating the incident, but the situation at the prison is calm now."
Al-Shabaab has carried out numerous bombings and other attacks in Mogadishu and other parts of the troubled country, although few have been recorded in recent months.
"There was an explosion inside the prison and a heavy exchange of gunfire followed," said witness Abdirahman Ali.
"I was very close to the prison when the incident occurred and I saw police enforcements entering the prison a few minutes after the gunfire broke out," he said.
Another witness, Shuceyb Ahmed, also reported hearing grenade blasts and gunfire.
"I called my brother who is a member of the prison guards, and he told me that several Al-Shabaab inmates secretly obtained weapons and grenades and tried to escape."
The Somali National News Agency (SONNA) published pictures of the dead bodies of the five alleged Al-Shabaab gunmen.
The Somali Custodial Corps is a branch of the security forces responsible for running prisons in the country.
Al-Shabaab has been fighting to oust the fragile central government in Mogadishu for more than 17 years.
The government has joined hands with local clan militias to fight the Islamist militants in a campaign supported by an African Union force and US air strikes.
But the offensive has suffered setbacks, with Al-Shabaab earlier this year claiming it had taken multiple locations in the centre of the country.