The militants blasted a hole in the perimeter wall with a car bomb and engaging in a fierce gun battle with African peacekeepers, police said.
The battle was still raging at the house of Somali's top military commander, General Dahir Aden Indha Qarshe, near the presidential palace, Abdikadir Ahmed, a senior police officer, told Reuters.
Mogadishu has been hit by a series of suicide bomb attacks in the past few weeks, claimed by al Qaeda-linked militants al Shabaab, who were pushed out of the city about two years ago but have waged a sustained guerrilla campaign.
"People cannot go near. There is a house near the palace under attack, the fighters are inside and AMISOM (African Union peacekeeping troops) are firing bullets and shells," Ahmed said.
"A car (laden) with explosives entered the presidential palace (compound) from the side of the military commander's house," he said.
"The car bomb hit and exploded and other al Shabaab cars with armed men drove inside the palace, and heavy fighting is still going on. There is shelling and heavy exchange of fire," he added.
The rebels were pushed out of Mogadishu in 2011 by African Union peacekeeping troops but have carried out several major attacks on high profile targets, denting gradual security improvements in Mogadishu.
At least seven Somalis were killed when a remote-controlled bomb aimed at a United Nations convoy tore through cars and tea shops just outside the capital's international airport last week.
SOMALI PRESIDENT UNHARMED IN ATTACK ON PALACE
Somali president Hassan Sheikh Mohamud was unharmed after an attack on the heavily fortified presidential palace in Mogadishu, known as Villa Somalia, the UN envoy to the Horn of Africa country said.
"President just called me to say he's unharmed. Attack on Villa Somalia had failed. Sadly some lives lost," UN Special Representative Nick Kay wrote on his official Twitter feed.