(CNN) -- Islamist rebels dressed in government military uniforms approached the gate of a high security prison in Mogadishu on Sunday, set off a car bomb and fired their way into the building, eyewitness Farah Mohammed told CNN.
They were Al-Shabaab fighters, and security guards at the prison "foiled the attack" and killed seven of the rebels, Somalia's Information Minister H.E. Mustafa Duhulow said in a statement.
"The attackers exchanged heavy gunfire with prison guards after detonating a car bomb at the main gate," Mohamed said. "A plume of dark smoke could be seen rising from the attacked complex."
During the assault on the National Intelligence and Security Agency prison three security guards and two civilians were also killed, 15 others were injured, Security Ministry spokesman Mohamed Yusuf told CNN.
The attackers tried to free their fellow Al-Shaabab members held at the prison, who were sentenced to death by a Somali military court tribunal, said police Officer Abdifarah Ali.
"We were behind today's raid on NISA prison in Mogadishu," Al-Shabaab spokesman Sheikh Abdiaziz Abu Musab said on pro-militant radio station Al-Andalus. "Our Mujahideen forces stormed the complex and then sprayed the prison guards with bullets and bombs," he said.
The government praised the security forces, saying it shows that Somalia has improved security.
"These terrorists groups are against the security improvements we are currently experiencing here in Mogadishu," Information Minister H.E. Mustafa Duhulow said in a statement. "We say to them that these foiled attacks strengthen our forces and prove their bravery to the people of Somalia," he said.
The NISA prison is underground and is close to the Somali presidential palace in Mogadishu. Hundreds of inmates, mostly Al-Shabaab members, are being held there.
Al-Shabaab is an al Qaeda-linked militant group seeking to turn Somalia into a fundamentalist Islamic state, according to the Council on Foreign Relations.
The group claimed responsibility for the deadly attack at a Kenyan mall last year and is believed to be responsible for attacks in Somalia that have killed international aid workers, journalists, civilian leaders and African Union peacekeepers.
CNN's Nana Karikari-apau, and Kay Guerrero contributed to this story.