A suicide bomber has killed at least 21 people at a funeral in the Iraqi capital, while an ambush has halted the government forces' advance on a key northern city controlled by ISIL fighters.

Sunday's attack, which also wounded 35 people, occurred outside a Shia house of worship in the western Baghdad neighbourhood of Harthiya, where people were attending a funeral service, a local police officer and a medical official told Reuters news agency.

Live Box 201471121759854996

Baghdad has witnessed a surge in bombings in the past month, most of them claimed by ISIL, or the Islamic State in Iraq and the Levant, as the government, headed by Prime Minister Haider al-Abadi, seeks traction in its effort to subdue Sunni-dominated parts of the country seized by the group.

"The attacker approached the entrance of the mosque and blew himself up among the crowd," the police officer said.

Accounts differed as to whether the bomber struck at the gate or inside the mosque, where some of the sources told the AFP news agency that mourners were gathered at the time of the explosion.

There was no immediate claim for the attack, but suicide bombings are almost exclusively carried out by Sunni fighters, including members of ISIL.

In further violence on Sunday, a roadside bomb exploded near a security patrol in the Tarmiyah area in the north of Baghdad, killing at least four people and wounding at least eight, officials said.

Offensive foiled

Elsewhere in Iraq, government forces attempted to retake the northern city of Baiji, which is adjacent to the country's largest refinery still under government control, despite a siege by ISIL.

The military operation, launched in the early hours of Saturday, was foiled when an armoured vehicle blew up near the security forces' convoy in a village in about 20km south of Baiji, officers said.

Live Box 2014108135930757185

The blast killed four soldiers and wounded seven.

"The attacker surprised our forces as he was driving a military armoured vehicle. We thought it was our vehicle," said an army major participating in the operation.

"We are planning to retake Baiji as soon as possible to secure a key highway and to stop the daily attacks of terrorists on the Baiji refinery," he said.

The offensive looks to bypass the Iraqi city of Tikrit, which lies to the south of Baiji and is controlled by ISIL, and instead to focus on Baiji itself.

Iraqi forces have protected the Baiji refinery since June despite being surrounded on all sides after the Iraqi army imploded in the north in the face of a major ISIL assault.

ISIL holds territory across eastern Syria and northern and western Iraq, with the ambition of establishing rule based upon what it describes as Islamic precepts.

The US is leading an international coalition, conducting air strikes aimed to defeat the group.

Source: Agencies