Gunmen attacked a cafe with gunfire and grenades north of the Iraqi capital and later detonated suicide belts against security forces in pursuit, officials said on Friday.
The deadly raid targeted a cafe that was popular with local fans of Real Madrid football club and drew condemnation from the Spanish government.
The club even announced that its players would wear black armbands for their league game on Saturday.
At least 16 people were killed and 30 wounded, including several members of the security forces, in the attack in the town of Balad and the ensuing chase, they said.
Officials said the gunmen were from the Islamic State group, which claimed the attack in an online statement.
"A group of armed men from Daesh (an Arabic acronym for IS) disguised in security forces uniforms attacked a cafe in Balad at around midnight, using grenades and gunfire," Ammar Hekmat al-Baldawi, a deputy governor of Salaheddin province, told AFP.
"They fled to a nearby farming area. They blew themselves up when the security forces and some residents chased them and got close," he said.
Baldawi said the attack took at place around midnight (2100 GMT on Thursday) and that the hunt for some of the attackers was ongoing.
"There were five or six attackers -- four of them blew themselves up. The hunt is not over because we think there could be up to two of them still on the run," he said.
He said 16 people were killed, including several members of the police and the Hashed al-Shaabi paramilitary organisation that includes most Shiite militias.
The modus operandi was a departure from the suicide car bombings IS has used as its main tactic to inflict maximum casualties in Shiite towns and cities.
- Spanish condemnation -
Balad is a large town 80 kilometres (50 miles) north of Baghdad . Its population is predominantly Shiite but it is surrounded by many Sunni-dominated rural areas.
A police lieutenant colonel who declined to give his name said the shooting claimed 12 lives, and added that four other people, two police and two members of Hashed forces, were killed later when one attacker detonated his suicide belt.
A doctor at Balad hospital gave the same toll.
The Spanish foreign ministry issued a statement condemning "in the strongest terms the terrorist attack carried out today against a Real Madrid supporters club".
"This is a particularly heinous act because the aim of the terrorists were civilians who gathered for the sole purpose of following a sporting event," the statement said.
The president of the Spanish Football League , Javier Tebas, also reacted by saying "football continues to be a target of terror".
The cafe used by the Real Madrid fans as a regular gathering place was known by residents as one of the few in the area to be open late, and there was no suggestion that the attackers targeted it for hosting football enthusiasts.
Real Madrid club also issued a statement condemning the attack and announced that its players would "wear symbolic black armbands as a sign of their mourning and respect" for their Saturday league fixture.
In its statement posted on social media, IS said a commando of three jihadists stormed a gathering of Hashed members, shooting several dead, before one of them blew himself up as rescue services tried to evacuate the wounded.
It said the two other attackers killed several others when they detonated their explosive belts.
The jihadist organization said the attack was the latest in a campaign to honour Abdel Rahman Mustafa al-Qaduli, the group's second-in-command, who was killed in a coalition strike in March.
IS has been steadily losing ground to Iraqi forces on the battlefield lately, but has upped its campaign of bombings against civilians.
At least 94 people were killed in three suicide attacks in Baghdad on Wednesday, the deadliest day in the Iraqi capital this year.