Iraq's prime minister, Haider al-Abadi, arrived in Mosul on Sunday and congratulated the armed forces for their victory over the Islamic State (IS) group after eight months of urban warfare, bringing an end to three years of militant rule in the city.

The battle has left large parts of Mosul in ruins, killed thousands of civilians and displaced nearly one million people.

"The commander in chief of the armed forces Haider al-Abadi arrived in the liberated city of Mosul and congratulated the heroic fighters and Iraqi people for the great victory," his office said in a statement.

A photo on Abadi's official Twitter account showed him dressed in a black military uniform and cap as he arrived in Mosul to announce the recapture of the city.

PM Al-Abadi arrives in Mosul to announce its liberation and congratulate the armed forces and Iraqi people on this victory pic.twitter.com/bUtkj7z88A — Haider Al-Abadi حيدر العبادي (@HaiderAlAbadi) July 9, 2017

The French president, Emmanuel Macron, whose country is part of an international coalition which backed the campaign with air strikes, training and assistance on the battlefield, welcomed the defeat of IS in Mosul.

"Mosul liberated from ISIS: France pays homage to all those, who alongside our troops, contributed to this victory," Macron said on his Twitter account.

Gunfire still audible

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 the statement.

The decaying corpses of militants lay in the narrow streets of the Old City, where IS has staged a last stand against Iraqi forces backed by a US-led coalition.

The group vowed to "fight to the death" in Mosul, but Iraqi military spokesman Brigadier General Yahya Rasool told state TV earlier on Sunday that 30 militants had been killed attempting to escape by swimming across the River Tigris that bisects the city.

Cornered in a shrinking area, the militants have resorted to sending women suicide bombers among the thousands of civilians who are emerging from the battlefield wounded, malnourished and fearful.

Battle for Mosul: Terrifying final moments of house-to-house advances, air strikes Read More »

The battle has also exacted a heavy toll on Iraq's security forces.

The Iraqi government does not reveal casualty figures, but a funding request from the US Department of Defense said the elite Counter Terrorism Service, which has spearheaded the fight in Mosul, had suffered 40 percent losses.

The United States leads an international coalition that is backing the campaign against IS in Mosul by conducting air strikes against the militants and assisting troops on the ground.

The Department of Defense has requested $1.269bn in US budget funds for 2018 to continue supporting Iraqi forces.

Without Mosul, by far the largest city to fall under militant control, IS's dominion in Iraq will be reduced to mainly rural, desert areas west and south of the city where tens of thousands of people live.

Iraqi woman and her children who fled fighting between government forces and Islamic State militants on Saturday in Old City of Mosul, sit in city's western industrial district waiting to be relocated (AFP)

Three years after 'caliphate' declared

It is almost exactly three years since the ultra-hardline group's leader, Abu Bakr al-Baghdadi, proclaimed a "caliphate" spanning Syria and Iraq from the pulpit of the medieval Grand al-Nuri mosque.

Abadi declared the end of Islamic State's "state of falsehood" a week ago, after security forces retook the mosque, although only after retreating militants blew it up.

The United Nations predicts it will cost more than $1bn to repair basic infrastructure in Mosul. In some of the worst affected areas, almost no buildings appear to have escaped damage, and Mosul's dense construction means the extent of the devastation might be underestimated, UN officials said.

The militants are expected to revert to insurgent tactics as they lose more territory.

The fall of Mosul also exposes ethnic and sectarian fractures between Arabs and Kurds over disputed territories or between Sunnis and the Shia majority that have plagued Iraq for more than a decade.

Members of the Emergency Response Division are seen in the Old City of Mosul (Reuters)

Oxfam cautioned that residents must be treated equally, regardless of any alleged family links to IS.

"As Mosul rebuilds, all its residents must enjoy the same rights and opportunities – whatever their gender, religion, or ethnic or tribal affiliation. In particular, Mosul residents must not face discrimination or punishment because of family links to ISIS fighters," Oxfam’s Country Director in Iraq, Andres Gonzalez, said in a statement.

"The retaking of Mosul will no doubt inspire hope among many Iraqis: hope that they can return home, rebuild their lives, and heal the divisions within their society. But these hopes will not be realised quickly or easily. Mosul residents continue to face severe risks from revenge attacks and explosives, and a lack of clean water, healthcare, and other basic services."