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.
"Violent elements tried to stir terror in the central prison," Colonel Abdiqani Khalaf, spokesman for the Somali Custodian Corps, said at a brief press conference, adding that the situation has now returned to normal.
"Five prisoners and three soldiers died and 18 other prisoners and three soldiers were wounded," he said.
"Investigations are ongoing and the wounded are being treated now."
Witnesses living near the compound reported hearing the sound of grenade explosions and machinegun fire.
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.
TWEET YOUR COMMENT