BAGHDAD — Government forces appeared close to capturing the capital of Iraq’s largest province from the Islamic State on Monday, dealing a potentially significant blow to the militant group as it loses territory in both Iraq and Syria.

Soldiers and counterterrorism troops stormed into a sprawling government facility in Ramadi, driving the militants out of the area and effectively ending their seven-month occupation of the city, Iraqi officials said.

Television images showed the troops celebrating after their advance, which was aided by airstrikes from a U.S.-led coalition, by raising the Iraqi flag over the compound and slaughtering sheep inside of it.

The compound was more symbolic than strategic, but its change of hands appeared to be the decisive blow to the militant group’s hold on the city. Now, government forces appear poised to press their offensive: Prime Minister Haider al-Abadi, in a statement congratulating his forces for “defeating” the Islamic State in Ramadi, vowed to take the fight to the group in the country’s second-largest city.

“We are coming to liberate Mosul,” Abadi said.

The Islamic State shocked Iraqis in May when it captured Ramadi, capital of Anbar province. Losing the city would represent one of the most dramatic setbacks suffered by the group since its lightning assault across Iraq in June 2014.

“Daesh are running away now and all the city is under our control,” said Maj. Gen. Hadi Rzaig, head of the Anbar police force. Daesh is the Arabic acronym for the Islamic State, which is also known as ISIS and ISIL.

Secretary of State John Kerry commended Iraq’s government and military for their fight “to return the capital of Anbar province back to the Iraqi people.”

“While Ramadi is not yet fully secure and additional parts of the city still must be retaken, Iraq’s national flag now flies above the provincial government center and enemy forces have suffered a major defeat,” he said in a statement.

The operation to retake Ramadi has produced intense fighting and caused vast destruction in the city, which had a population of more than a million people before the Islamic State takeover. It is unclear how many Iraqi forces and civilians have been killed in the battles, which involved fending off the militant group’s waves of suicide bombers.

The governor of Anbar province, Souhaib al-Rawi, estimated that 1,000 Islamic State militants had been killed during months of grinding assaults to retake Ramadi. He called the capture of the government compound “a victory.”

Rebuilding Ramadi, if it can be fully secured by the government, will be no easy task.

Suspicion of Iraq’s Shiite-dominated government runs high in the Sunni city, whose residents felt abandoned by officials in Baghdad as Islamic State militants mounted their assault in May. Lacking support from the government, Ramadi residents formed community defenses and even purchased their own weapons to defend the city. Islamic State militants killed scores of residents and exacted other forms of retribution on people who were associated with the government, including home demolitions.

But among the Iraqi forces in Ramadi on Monday, the mood was celebratory. Speaking to Iraqi television, Gen. Talib Shigati, a senior commander, thanked his troops and expressed confidence in their abilities.

The capture of Ramadi would mark the first time that Iraqi armed forces have seized a city from the Islamic State without the aid of the country’s powerful Shiite militias, which did not participate in the operation because of concerns about sectarian tensions with the city’s mostly Sunni inhabitants.

Lt. Gen Abdulghani al-Assadi, a commander of a counterterrorism unit in the city, said that seizing control of the sprawling compound – which contains provincial and municipal government offices – gave his forces the decisive upper hand. That forced most of the militants in Ramadi to flee, but he warned that some neighborhoods had “pockets” of apparent Islamic State militants that still had to be confronted, he said.

“We are clearing out the city of booby traps and bombs, but the remaining Daesh fighters are in retreat,” Assadi said, describing the operation as “a historic moment for the Iraqi people and for the Iraqi armed forces.”

The push into Ramadi, about 80 miles west of the capital, Baghdad, underscores the flagging battlefield momentum of the Islamic State. The group has been losing control of territory in Iraq and Syria recently to U.S.-backed Kurdish and Arab opponents.

Army Gen. Lloyd Austin, the chief of U.S. Central Command, congratulated Iraq’s security forces on securing the government complex in Ramadi, calling it “an important operational achievement.” He stopped short of calling it a strategic success, however, perhaps a nod to the tenuous security situation that remains in the city.

Col. Steve Warren, a U.S. military spokesman in Baghdad, said in a different statement that the U.S.-led coalition fired more than 630 airstrikes to help Iraqi forces advance on Ramadi. Those forces also received help in clearing improvised-explosive devices and other bombs that the Islamic State deployed against coalition-aligned forces, he said.