Three of the eight attackers were killed in the operation, an interior ministry spokesman said.
Security forces were quick to seal off the surrounding area, also home to the interior and transport ministries.
No group has claimed responsibility, though militants linked to al-Qaeda have mounted similar attacks.
The assault on the offices of the transport ministry along Canal Street in the northeast of the capital comes as security forces grapple with intensifying violence and an extended standoff with anti-government fighters in western Anbar province.
It was not immediately clear if any casualties had resulted from the militant attack.
"A terrorist group infiltrated the company, which is next to our building," said Kamal Amin, spokesman for the human rights ministry, which has offices next door.
Elsewhere in the Iraqi capital on Thursday, bombings near a market and a restaurant in the neighborhoods of Kasra and Talbiyah killed six people, security and medical officials said.
They struck just hours after several car bombs ripped through Baghdad Jadidah, Shuala and Talbiyah, leaving nine people dead on Wednesday evening.
Attacks on Wednesday also hit the outskirts of the capital, as well as the northern cities of Mosul and Tuz Khurmatu, killing seven others.
At least 909 people have been killed so far this month as a result of violence in Iraq, according to reports from security and medical officials.
NJF/NJF