The fighting lasted about an hour on Thursday, ending with security forces storming the building, killing the gunmen and evacuating hundreds of people who had hunkered down in their offices, according to police.
There was no immediate claim of responsibility, but the attack bore the hallmarks of al-Qaeda's Iraqi arm. The group, known as the Islamic State of Iraq, frequently uses car bombs and coordinated blasts in an effort to undermine Iraqis' confidence in the Shiite-led government.
The attack erupted shortly after midday in Baghdad's Allawi area. The largely commercial area is home to Iraq's National Museum and the city's main bus station.
At least two blasts, including one car bomb and another believed to be from a suicide bomber, went off near a building currently housing the justice ministry. A police officer who was among the troops sent to clear the area said that approximately six gunmen wearing police uniforms quickly stormed the building.
"Everybody panicked [after the first blast] and seconds later we heard a second explosion. I looked through the window and I saw some gunmen wearing police uniforms entering the building. We knew that these policemen were fake," said Asmaa Abbas, a justice ministry employee who was working in her third-floor office.
A gun battle quickly broke out between the intruders and security forces, as other explosions went off near the bus station and the headquarters for a VIP protection force that provides bodyguards for lawmakers, government ministers and other senior officials.
After about an hour, security forces stormed the building and some of the gunmen detonated explosives they were wearing, the officer on the scene said.
"It was the longest hour in my life," said Abbas, the employee.
Deputy Justice Minister Busho Ibrahim said there were more than 1 000 people in the four-story building at the time of the attack. He said the minister is abroad and was not inside.
"When the explosions and shooting started, the guards evacuated me out a back door, and I have no idea what happened after that," he said, speaking over the telephone from outside the building.
The attack killed 22 people in addition to the gunmen and wounded 55 others, police said. The dead include seven police officers.
Hospital officials confirmed the casualty numbers. The officials spoke on condition of anonymity because they were not authorised to speak to the media.
Justice ministry employees had moved to this facility after a 2009 attack on the nearby ministry of justice, which is now being repaired. That attack was part of a double car bombing which killed at least 147 people and heavily damaged the building.
Thursday's attack took place about a kilometre away from the heavily fortified Green Zone, which houses several foreign embassies and Iraq's government offices.
Frequent deadly attacks
Violence in Iraq has subsided from its peak in 2006 and 2007, but deadly attacks remain frequent a decade after the US-led invasion of March 20 2003.
Iraq's government is being challenged by weekly protests that began in December from Sunnis angry over perceived discrimination. The demonstrations have been largely peaceful and most Iraqi Sunnis do not voice support for al-Qaeda.
But al-Qaeda hopes to exploit the increasing tensions. It believes Shi'ites are heretics and that Iraq's government is too closely allied with neighbouring Shi'ite powerhouse Iran.
Earlier this week, al-Qaeda claimed responsibility for the deaths of 51 Syrian soldiers and nine Iraqis in a well-planned assault in west Iraq on March 4, intensifying concerns that the terror group is coordinating with Islamist rebels fighting to topple Syria's President Bashar Assad. – Sapa-AP