Three of the attackers were killed and a fourth captured, the minister added.
Al-Shabab fighters entered the compound in the capital, Mogadishu, on Tuesday before being driven out by African Union (AU) and government forces.
This was the second assault on the heavily fortified compound this year.
The police and intelligence heads – Abdihakim Saaid and Bashir Gobe respectively – have been replaced with immediate effect, said Information Minister Mustafa Dhuhulow.
Somali soldiers stand near the wreckage of a car bomb that was detonated at the main gate of the presidential palace in Mogadishu, Somalia, Wednesday, 9 July 2014
The militants have vowed to step up attacks during the Islamic holy month of Ramadan
President Hassan Sheikh Mohamud was not at the palace at the time of the attack.
Ads by OnlineBrowserAdvertisingAd Options
Al-Shabab said 14 government soldiers had been killed in the assault but government officials have not referred to this claim.
The raid started with a suicide bomber detonating a car at a barrier near the entrance to the compound, after which the militants attacked from two directions, correspondents say.
At a press conference at the palace after the attack was repelled, Mr Mohamud said the government would not be intimidated by al-Shabab.
“I say to them, you will not kill us, and nor will you demolish our spirit,” he added.
Some 22,000 African Union troops are helping the UN-backed government battle al-Shabab, which wants to create an Islamic state.
Al-Shabab lost control of Mogadishu in 2011, but often carries out attacks in the city.
The group has vowed to step up attacks during the Islamic holy month of Ramadan.
On Saturday, a suicide car bomb exploded near the parliament building, killing at least four people.