The attack - by the al Qaeda-linked group that killed 67 people at a Kenyan shopping mall last year - started with a car bomb at a gate to the heavily fortified parliament compound, followed by a suicide bombing and then a gun battle that continued for hours.
"Ten government forces died and 14 others were injured in the attack today. Four lawmakers were also injured. Seven of the fighters who attacked the house were also killed as you see their bodies," Kasim Ahmed Roble, a police spokesman, told reporters at the scene.
Roble made no mention of any civilian casualties.
A spokesman for al Shabaab, Sheikh Abdiasis Abu Musab, said the group's fighters had killed 30 people. "We killed 30 from the AU (African Union) and from the various forces of the so-called Somali government," he said.
The al Shabaab estimate of the death toll was not independently verifiable.
Reuters witnesses saw four bodies at the scene and a soldier fall from a rooftop after being shot. Reuters television pictures showed a large pool of blood near a blast site, and a man with his shirt drenched in blood running away from the scene.
The fighting continued for hours after the initial explosion, with gunfire and smaller blasts being heard around the parliament.
"We are behind the suicide bombing, explosions and the fighting inside the so-called Somali parliament house, and still heavy fighting is going on inside," said the al Shabaab spokesman.
The African Union Mission in Somalia said in a statement that all the lawmakers who were in parliament before the attack were safely evacuated.
The attack on parliament, a building about 300 metres (yards) from the president's palace that is guarded by African Union peacekeepers and Somali forces, showed that the al Qaeda-linked group remained capable of hitting the centre of Mogadishu despite being pushed out of the capital two years ago.
"The terrorists have once again shown that they are against all Somalis, by killing our innocent brothers and sisters. These cowardly, despicable actions are not a demonstration of the true Islamic faith," said Prime Minister Abdiweli Sheikh Ahmed.
The U.S. State Department strongly condemned the attack, offering condolences "to those affected by this heinous act of terrorism."
"We continue to stand firmly with the Federal Government of Somalia and the many international partners working to support its efforts to root out the threat posed by al-Shabaab," State Department deputy spokeswoman Marie Harf said in a statement. "Cowardly acts such as these will not shake our resolve."
Nicholas Kay, the U.N. secretary-general's special representative for Somalia, said: "The Federal Parliament represents the people of Somalia and their hopes and aspirations for a peaceful and stable future. Today's attack is an attack against the people of Somalia for which there can be no justification."
LOSING CONTROL
Somalia's government is struggling to impose any sense of order, more than two decades after the fall of dictator Mohamed Siad Barre tipped the country into chaos.
In February, at least 11 people were killed when al Shabaab attacked the presidential compound. In April, it killed two lawmakers.
A Western diplomat who has worked with regional intelligence agencies said the attack would add to pressure on President Hassan Sheikh Mohamud from about 100 parliamentarians who last month called for him to be impeached over worsening security.
"The federal government is exercising no control," the diplomat said. "Those ... in parliament will start asking questions: What is this guy achieving?"
The diplomat said the attack showed that a surge by the African Union peacekeeping troops had not weakened al Shabaab's capacity to wage asymmetric warfare in the capital, where coordination between Somali and foreign intelligence agencies is poor.
"Because intelligence is fragmented, al Shabaab is slipping through the net," said the diplomat. "They are becoming more dangerous."