Clashes broke out in Tarmiya, an agricultural area about 30 kilometres (19 miles) from the capital where Islamic State (IS) group fighters operate.
The raid came as hundreds of thousands of pilgrims descended on the Kadhimiya shrine in north Baghdad of Mussa Kadhim, seventh of the 12 imams revered by Shias.
An army unit launched a "raid on a hideout of IS terrorists", the Iraqi government's security media cell said, adding that "three terrorists were killed, one of whom was wearing an explosive belt".
"Two officers and two soldiers" were also killed when the device was detonated, the statement said.
An interior ministry official speaking on condition of anonymity said seven other soldiers had been wounded during the raid.
There was no immediate statement from IS claiming the deaths on the group's usual Telegram channels.
Iraq declared in late 2017 that the militant group had been defeated but members and sleeper cells continue to operate in various parts of the country.
On December 19, the group launched an attack on the village of Albu Bali north of Baghdad, killing eight civilians.
Days earlier, a roadside bomb hit a military vehicle and killed three soldiers in farmland north of Baghdad.
The United Nations has said that despite setbacks, the group has "maintained its ability to launch attacks at a steady pace".
It estimates the jihadist group maintains between 6,000 and 10,000 fighters inside Iraq and Syria, exploiting the porous border between the two countries and concentrating mainly in rural areas.