The blast happened Saturday afternoon when a group of soldiers searched a farmhouse in Garma, an area near the city of Fallujah, 65 kilometres west of the capital, police said. Minutes later, police said gunmen opened fire on arriving soldiers.
The blast wounded 24 soldiers and leveled the home, officials said.
In other attacks, police said a roadside bomb targeting a military convoy killed three soldiers and wounded seven just north of Baghdad.
Medical officials at two Baghdad hospitals confirmed the casualties. All officials spoke on condition of anonymity as they weren't authorized to speak to journalists.
More From This Section
The takeover was sparked when security forces arrested a Sunni lawmaker sought on terrorism charges, then dismantled a year-old Sunni anti-government protest camp. They later pulled out of the area to calm angry residents, allowing the militants to take control.
Violence has been escalating in Iraq. Last year, Iraq saw its highest death toll since the worst of the country's sectarian bloodletting began to subside in 2007, according to United Nations figures.
The country will hold its first parliamentary elections since the withdrawal of US troops on April 30.