The Islamist group and police said.
"A car bomb exploded at the gate of the parliament house - then it was followed by a suicide bomber explosion. So far we have confirmed four policemen dead," a police colonel, Farah Hussein, told Reuters.
"The lawmakers and the other workers were rescued as soon as the car bomb exploded. But the terrorists are still firing from inside a mosque nearby," he added.
A Reuters witness who saw four bodies on the ground and one wounded man fleeing the scene said the fighting was ongoing.
Al Shabaab, the al Qaeda-affiliated group that was pushed out of the capital about two years ago and has since waged a sustained guerrilla campaign, said it had launched the attack.
"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," Sheikh Abdiasis Abu Musab, al Shabaab's spokesman for military operations, told Reuters.
Since being ousted from Mogadishu, al Shabaab has intensified its attacks in Somalia and in neighbouring Kenya, which sent troops across the border to fight the Islamists. In September, the group killed 67 people in a shopping mall in the capital Nairobi.
In February at least 11 people were killed when it attacked the presidential compound.