MOSUL, Iraq (AP) — Prime Minister Haider al-Abadi declared victory Monday evening over the Islamic State in Mosul after nearly nine months of grueling combat to drive the militants out of Iraq's second-largest city.

"We announce the total victory for Iraq and all Iraqis," al-Abadi said, speaking from a small base in western Mosul on the edge of the Old City, where the last pockets of resistance had been holding out.

"This great feast day crowned the victories of the fighters and the Iraqis for the past three years," he said.

Hours earlier, airstrikes pounded the last IS-held territory on the western edge of the Tigris River. In recent days, Iraqi troops, closely backed by airstrikes from the U.S.-led coalition, confined the remaining few hundred extremists in an area measuring less than a square kilometer (less than a mile).

Shortly after al-Abadi's speech, the coalition congratulated him on the victory against "a brutal and evil enemy," according to a statement.

"While there are still areas of the Old City of Mosul that must be back-cleared of explosive devices and possible ISIS fighters in hiding, the ISF have Mosul now firmly under their control," the statement added.

Al-Abadi was in Mosul on Sunday, congratulating Iraqi soldiers on recent gains but stopping short of declaring an outright victory.

The battle for Mosul was Iraq's longest and most punishing conventional fight against IS in the more than three-year war against the extremists.

Launched in October, the massive operation comprised more than 70,000 Iraqi troops drawn from the country's army, special forces, police, tribal fighters and mostly Shiite paramilitary forces.

Over the course of the campaign, Iraq's special forces units who largely led the assault have faced casualty rates of 40 percent, according to a report in May from the office of the U.S. secretary of defense.

Additionally, thousands of civilians were estimated to have been killed, according to Nineveh's provincial council. That did not include those still believed buried under collapsed buildings.

The fight also displaced more than 897,000 people, and the United Nations said there was no end in sight to the humanitarian crisis in Iraq despite the conclusion of the fight.

The U.N. said thousands of Mosul residents will likely remain displaced from the city after the fight is concluded because of "extensive damage caused during the conflict."

The battle also has decimated Mosul's infrastructure in its western half, where fighting was fiercest. Iraq's civil defense rescue teams — a branch of the Interior Ministry — said about 65 percent of the buildings in the Old City, many dating back centuries, were severely damaged or destroyed. In western neighborhoods like Zanjili, destruction was estimated to be 70 percent of all houses, buildings and infrastructure.

Mosul fell to IS militants within a matter of days in June 2014, starting a political and security crisis not seen in the country since the 2003 toppling of Iraqi leader Saddam Hussein

The territorial gains by the extremists led to the ouster of Iraq's top leaders, dramatically shifted the balance of power among its security forces, empowered Iranian-backed fighters who are now sanctioned by the central government, and brought U.S. ground troops back onto Iraqi soil for the first time since 2011.

The road to retake Mosul has taken the government, its security forces and the coalition more than three years of training troops to replace the tens of thousands of Iraqi forces who dissolved in the face of the 2014 IS advance.