"A group from the Daesh terrorist gang tried to infiltrate Samarra and forces confronted them and killed them," the Joint Operations Command said in a statement, using an Arabic acronym for the Islamic State group.
Samarra, which lies 110 kilometres north of Baghdad, is home to a major security headquarters and to an important Shiite shrine where a 2006 bombing touched off two years of sectarian bloodletting.
At least four members of the Iraqi security forces were killed and one wounded in the attack, the JOC and other security officials said.
A curfew was briefly slapped on the city as security forces combed the area for any additional attackers.
More From This Section
There was no immediate claim for the attack but officials in Samarra blamed it on IS, which is fighting to defend its Iraqi bastion of Mosul against a huge government offensive.
The "inghimasi" attack, a term for jihadist operations in which gunmen, often wearing suicide vests, intend to sow chaos and fight to the death rather than achieve any military goal, fits a recent pattern of IS attacks.