Updated

Somalia's government said its troops have retaken the presidential palace in the capital of Mogadishu Tuesday, after Islamic militants forced their way in and exchanged heavy gunfire with troops and guards.

Loud explosions and gunfire could be heard inside the heavily fortified compound, which has been the subject of many attacks by Al Qaeda-linked militants over the years. After more than two hours of fighting, Somalia's presidency said in a Twitter update that "the shameful attack" had been foiled by Somalia's armed forces and African Union peacekeepers.

It said the "attack is over" and that the compound has since been secured, but gave no details about any casualties or what happened to the militants.

Al-Shabab claimed responsibility for the attack, saying in a radio broadcast that its fighters had penetrated the presidential palace.

Somali President Hassan Sheikh Mohamud was not inside the palace at the time of the attack, but the prime minister and the speaker of parliament were, said Capt. Mohamed Hussein, a senior police official.

The presidency said both President Mohamud and Prime Minister Abdiweli Sheikh Ahmed were safe.

The attack was the second assault on the presidential palace this year, and it marked the first time militants have been able to breach the compound and take offensive positions inside. The palace is protected by government troops and African Union peacekeepers who helped to drive the al-Shabab militants out of their bases in Mogadishu in 2011.

The attack started when a car bomb exploded outside the presidential compound. Gunmen then entered the palace, police officer Mohamed Abdi told The Associated Press.

Police said the militants, who were armed with grenades, split up into groups once inside and tried to take control of different buildings in the compound.

Al-Shabab, which has links with Al Qaeda, has vowed to step up attacks during the Muslim holy month of Ramadan.

Al-Shabab militants have previously carried out deadly attacks against lawmakers, U.N. staff and government officials. Last year the group attacked an upscale mall in Nairobi, the capital of neighboring Kenya, which has sent troops to fight the militants under the African Union banner.

Militants killed four people Saturday when a car laden with explosives blew up near the parliamentary building in Mogadishu.