(Loveday Morris,Adam Taylor,Sarah Parnass/The Washington Post)

Iraq’s prime minister on Thursday declared the end of the Islamic State’s grip on Iraqi territory as government forces recaptured the site of a historic mosque in Mosul that had once been the symbolic center of the group’s self-declared caliphate.

Counterterrorism troops retook the area of the Great Mosque of al-Nuri and advanced through the remaining contested streets of Mosul, Iraq’s military said in a statement. The mosque itself was reduced to little more than rubble by the militants last week, but Prime Minister Haider al-Abadi said retaking it signaled the demise of the group’s “state” in Iraq.

The 12th-century mosque, famed for its leaning minaret, holds huge significance for the Islamic State as the place where, in July 2014, the organization’s leader, Abu Bakr al-Baghdadi, made his only known public ­appearance, calling on Muslims around the world to obey him as leader of the group’s newly created caliphate. Just weeks before, the militants had scored a stunning victory over Iraq’s armed forces in Mosul.

The militants had captured the city within days. Iraq’s battle to retake it has stretched on for nearly nine grueling months, and the military and the city’s civilians have suffered heavy casualties.

Once home to more than 2 million people, the city was by far the largest the militants ever controlled. Now, only about half a square mile of winding narrow streets in the ancient Old City has yet to be recaptured.

Iraqi commanders say they are facing stiff resistance as they fight house to house with militants who now have no way of escape.

“We will keep following Daesh until we kill and capture the last member,” Abadi said in the statement, using the Arabic acronym for the Islamic State.

But even as the battle of Mosul nears an end, the group is far from being eradicated. The Islamic State’s black flag still flies over the towns of Tal Afar to the west, and Hawijah to the south. The militants also control stretches of the border with ­Syria, where U.S.-backed forces are battling to oust the fighters from Raqqa, their main stronghold there.

Mosul has witnessed two major counterattacks in recent weeks, assisted by sleeper cells in areas supposedly long cleared of the militants, an indication of the remaining challenges.

Hundreds of thousands of ­people remain displaced, with the International Rescue ­Committee warning that “many difficult months lie ahead” for those who were forced to flee or lived under the Islamic State’s rule.

“Even once all territory has been cleared of ISIS fighters and unexploded mines, ISIS will continue to terrorize lives of ­people across Iraq,” said Wendy Taeuber, the group’s Iraq country director. “The horrors ISIS ­inflicted on the residents of ­Mosul left huge wounds, not only in the social fabric of the city but also in the minds of people.”

After a dawn assault, Iraq’s counterterrorism troops moved past the mosque but have not entered the remains of the ­building yet because it may be rigged with explosives, said Lt. Gen. Abdul Ghani al-Asadi, head of the force.

Asadi said he expected the ­battle to be over within a week. Counterterrorism forces were about to launch an offensive to retake the Nuri mosque a week ago when it was destroyed. The Iraqi military released video footage that it said showed the moment that the militants ­detonated explosive charges in the building and its minaret — nicknamed the hadba, or hunchback, for its distinctive tilt.

The Islamic State-affiliated news channel Amaq maintained that a coalition airstrike was ­responsible for the destruction of the mosque. Experts say that footage released by the Iraqi military appears to show a blast emanating from the building rather than an airstrike from above.