U.S. troops at the military compound to train Iraqi forces helped repel the attack, marking the first time American forces have been involved in an exchange of gunfire in Baghdad since the U.S. officially ended combat operations in Iraq less than a week ago.
In the brazen midday assault, suicide bombers set off a car bomb and managed to fight their way inside the building before being killed.
On Aug. 17, an al-Qaida-linked suicide bomber blew himself up at the same east Baghdad military headquarters and killed 61 army recruits in the deadliest act of violence in Baghdad in months.
Iraqi military spokesman Maj. Gen. Qassim al-Moussawi said in a statement that 12 people were killed in Sunday's attack and 36 were injured. Five Iraqi soldiers were among the dead.
An explosives-laden car charged the building and blew up. Then gunmen assaulted the headquarters, battling the building's guards in a 15-minute firefight, according to police officials who said at least three militants were wearing explosives belts.
The bombers were headed to the building's entrance on foot but were shot by the guards before they could trigger their devices.
One was wounded and has been taken into custody.
All officials spoke on condition of anonymity because they were not authorized to speak to the media.
The Americans provided covering fire while Iraqi soldiers pursued the attackers who had entered the compound, said Lt. Col Eric Bloom, a U.S. military spokesman. The U.S. intervention also included helicopters and drones, he said. The American troops were based in the compound to train Iraqi forces.
He said it was the first exchange of gunfire involving American troops in Baghdad since the Aug. 31 deadline for ending formal combat operations. No American troops were injured, and he said the American gunfire lasted a few minutes.
Sunday's attack was an embarrassment for the officials in the capital, where security has been high in past days as insurgents intensify their strikes on Iraqi police and soldiers to mark the change in the U.S. mission.
The building attacked on Sunday is the headquarters for the Iraqi Army's 11th Division and an army recruitment center. In mid-August attack, al-Qaida boasted that its operative easily passed through checkpoints before detonating his explosives belt in a crowd of officers and recruits outside the building.
The Iraqi security forces are now solely responsible for protecting the country after President Barack Obama declared an end to U.S. combat operations on Wednesday. Many, however, doubt that Iraq's police and army are a match for the well-armed insurgency determined to bring down the Shiite-led government.
Last week Prime Minister Nouri al-Maliki put his nation on its highest level of alert for terror attacks, warning of plots to sow fear and chaos in the country. He said insurgents would try to exploit widespread frustration with years of frequent power outages and problems with other public services by staging riots and attacks on government offices.
U.S. and Iraqi officials have long worried that political instability would lead to widespread violence in Iraq, and the lack of a power-sharing agreement among the competing leaders has only increased fears.
Six months after an inconclusive election in March, Iraq still has no new government. Al-Maliki, a Shiite, is struggling to keep his job after his political coalition came in a close second to a Sunni-dominated alliance at the March 7 vote.