"Nine attackers including two suicide bombers were killed when they tried to force their way into the presidential palace," Xinhua quoted Minister for National Security Abdelkarim Hussein Guled as saying.
Three were killed on the government side, including two officials and a soldier, he added.
A car bomb went off outside the gate of the presidential palace before a group of Al-Shabaab fighters tried to storm the building. The attack was aimed to cause maximum damage but was foiled by local troops and African Union peacekeepers, the minister said.
Somali President Hassan Sheikh Mohamoud, who was at the presidential palace at the time of the attack was unharmed and condemned the attack by terming it as "an act of terrorism".
Nine attackers, two suicide bombers were killed
"An act of terrorism, however unspeakable, does not hide the truth that this is a marginal group on the brink of extinction," the president said. "The military campaign we are fighting with brave Somali and AMISOM (African Union Mission in Somalia) soldiers will eliminate our enemies."
The attack on the presidential palace drew global condemnation.
UN Somalia envoy Nicholas Kay condemned the assault on the Somali presidency as "another desperate and criminal act" which "does nothing but harm to the people of Somalia".
"The UN and the international community remain steadfast in their determination to see a new Somalia rise, and continue to support Somalia in its quest to stabilise and rebuild institutions," Kay added. Head of the African Union Somalia mission (AMISOM) Mahamat Saleh Annadif condemned the attack on the presidential Palace as "cowardly".
"I commend the actions of the brave security forces who fought off the attackers, hell-bent on shedding the blood of innocent Somalis. The attack was against the Somali people, their institutions and their religion," Annadif said.