Iraq declared victory against the Islamic State group in Mosul on Sunday after a gruelling months-long campaign, dealing the biggest defeat yet to the jihadist group.

Iraqi Prime Minister Haider al-Abadi's office said he was in "liberated" Mosul to congratulate "the heroic fighters and the Iraqi people on the achievement of the major victory".

He was due to make an official speech soon.

The announcement comes after months of heavy fighting that have left Iraq's second city in ruins and forced tens of thousands from their homes.

The fighting did not seem to be completely over, with gunfire still audible in Mosul and air strikes hitting the city around the time the premier's office released its statement.

But Abadi's arrival had been expected for days as a signal of the end of the battle for the city.

Photographs released by his office showed the premier dressed in a black military uniform and cap, shaking hands with police and army officers as he arrived in the city.

The operation has been backed by a US-led coalition battling IS in Syria and Iraq, which has carried out waves of air strikes against the jihadists and deployed military advisers on the ground.

French President Emmanuel Macron, whose country is a key part of the coalition, was among the first world leaders to offer his congratulations.

"Mosul liberated from Daesh," he tweeted, using an Arabic acronym for IS. "Homage from France to all those, with our troops, who contributed to this victory."

Fleeing jihadists killed

IS swept across much of Iraq's Sunni Arab heartland in a lightning offensive in mid-2014, proclaiming a self-styled "caliphate" straddling Iraq and neighbouring Syria.

But the jihadist group, which is facing twin offensives backed by the US-led coalition in both countries, has since lost most of the territory it once controlled.

The Iraqi forces launched their campaign to recapture Mosul in October, seizing its eastern side in January and launching the battle for its western part the next month.

But the fight grew tougher when Iraqi forces entered the densely populated Old City on the western bank of the Tigris River that divides the city.

In recent days, security forces have killed jihadists trying to escape their dwindling foothold in Mosul, as Iraqi units fought to retake the last two IS-held areas near the Tigris.

Earlier Sunday Iraq's Joint Operations Command had said it killed "30 terrorists" trying to escape across the river.

Even in the final days of the battle, thousands of civilians remained trapped inside the Old City and those who fled arrived grief-stricken after losing relatives in jihadist sniper fire and bombardments.

Around 915,000 residents have fled Mosul since the start of the battle for the city in October, the United Nations said this week.

Not yet 'the death knell'

The recapture of Mosul will not however mark the end of the threat posed by IS, which holds territory elsewhere in Iraq and is able to carry out frequent bombings in government-held areas.

In Iraq it holds towns including Tal Afar and Hawijah in the north, as well as a string of territory in western Anbar province.

In also continues to hold significant territory in Syria, including its de facto Syrian capital Raqa, where a US-backed Kurdish-Arab alliance is battling to oust the jihadist group after penetrating its fortified historic centre.

Analysts warned that while the loss of Mosul was a major blow to the jihadists it was not yet a fatal one.

"We should not view the recapture of Mosul as the death knell for IS," said Patrick Martin, Iraq analyst at the Institute for the Study of War.

"If security forces do not take steps to ensure that gains against IS are sustained for the long-term, then IS could theoretically resurge and recapture urban terrain," he said.