MOSUL, Iraq -- Prime Minister Haider al-Abadi celebrated with Iraqi troops Sunday in Mosul after they drove Islamic State of Iraq and Syria (ISIS) militants from some of their last strongholds in a nearly nine-month campaign, although heavy fighting by holdouts continued in parts of the Old City neighborhood.

Dressed in a black military uniform, a smiling al-Abadi walked amid the soldiers, at one point grabbing an Iraqi flag and briefly draping it on his shoulders. Other troops waved flags and pointed their weapons in the air nearby.

Images posted to al-Abadi's official Twitter account Sunday showed the leader shaking hands with members of the armed forces.

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

PM Al-Abadi meets with ISF commanders and forces who led the Mosul liberation campaign pic.twitter.com/qLILtkIWch — Haider Al-Abadi (@HaiderAlAbadi) July 9, 2017

Al-Iraqia TV quoted al-Abadi as saying he "congratulates the heroic fighters and the people on the big victory" in Iraq's second-largest city, even as fighting rang out in pockets near the militants' last stand at the Tigris River.

Get Breaking News Delivered to Your Inbox

The loss of Mosul would mark a major defeat for ISIS, which has suffered a series of major setbacks over the past year.

The militants control less than a square kilometer, less than a mile, of territory in the shattered city in northern Iraq, but they were using human shields, suicide bombers and snipers in a fight to the death.

ISIS militants seized Mosul in the summer of 2014 when they swept across northern and central Iraq. That summer, the group's leader, Abu Bakr al-Baghdadi, appeared at Mosul's al-Nuri Mosque and declared a caliphate on territory it seized in Iraq and Syria.

Iraqi Prime Minister Haider al-Abadi, right, is pictured in Mosul Iraq, July 9, 2017. Iraqi Prime Minister Media Office/Handout via REUTERS

Iraq launched the operation to retake Mosul in October, backed by airstrikes from the U.S.-led coalition. The fierce battle has killed thousands and displaced more than 897,000 people.

Last month, as Iraqi troops closed in on Mosul's Old City, the militants destroyed the al-Nuri Mosque and its famous leaning minaret to deny the forces a symbolic triumph.

Lt. Gen. Jassim Nizal of the army's 9th Division said his forces achieved "victory" in their sector, after a similar announcement by the militarized Federal Police. His soldiers danced to patriotic music atop tanks even as airstrikes sent up plumes of smoke nearby.

Nizal acknowledged that many of his men were among those who fled the city when ISIS forces seized Mosul in 2014 in a humiliating defeat for the Iraqi armed forces. "Some things happened here, that's true," he said. "But we have come back."

Much of the Old City and surrounding areas have been devastated by the grueling urban combat. On Sunday, a line of weary civilians walked out of the Old City, past the shells of destroyed apartment blocks lining the cratered roads.

Heba Walid held her sister-in-law's baby, which was born into war. The parents of the 6-month-old, along with 15 other family members, were killed last month when an airstrike hit their home. When Walid ran out of formula, she fed the baby a paste of crushed biscuits mixed with water.

In neighboring Syria, U.S.-backed Syrian forces have encircled and pushed into ISIS' de facto capital of Raqqa after a month of fighting, although a long battle lies ahead.

More than 2,000 militants are holed up with their families and tens of thousands of civilians in Raqqa's center, the city's most densely populated districts.

The extremists still hold several smaller towns and villages across Iraq and Syria.