Updated

Iraqi government forces on Friday drove Islamic State militants out from their remaining strongholds inside the oil refinery town of Beiji, two security official said, in a key victory over the terror group that has captured much of northern and western Iraq in a stunning summer offensive.

The Iraqi troops, backed by allied Sunni militiamen, also lifted the Islamic State group's siege of the oil refinery, Iraq's largest, and hoisted Iraq's red, white and black flags atop the sprawling complex hosting the facility.

Reached by telephone in Beiji, the officials said the army used loudspeakers to warn the small number of residents still holed up in the town to stay indoors while bomb squads detonated booby-trapped houses and remaining bombs planted on the roads. The officials spoke on condition of anonymity because they were not authorized to speak to the media.

State Iraqi television also reported the "liberation of Beiji," quoting the top army commander there, Gen. Abdul-Wahab al-Saadi.

Islamic State extremists captured Beiji during their summer offensive. The Iraqi forces had collapsed in the face of that onslaught but have since partially regrouped and went on the offensive, with Beiji the biggest locality they have recaptured to date.

Since August, the Iraqi military is being aided by airstrikes by a U.S.-led coalition targeting Islamic State positions in Iraq and in neighboring Syria.

The two officials said fierce battles were fought early on Friday around the refinery and that government warplanes strafed Islamic State positions around the facility on the northern edge of the town. The refinery's capacity of some 320,000 barrels a day accounts for a quarter of Iraq's refining capacity.

Beiji will now likely be a base for staging a push to take back from the Islamic State Saddam Hussein's hometown of Tikrit to the south.

Government forces tried to retake Tikrit earlier this year, but their campaign stalled and the city remains in Islamic State hands.

The Beiji developments came just two days after Shiite Prime Minister Haider al-Abadi ordered a major shakeup in the military in what an official government statement described as an effort to eradicate corruption and improve its capabilities.

On Wednesday, 26 officers were relieved of duty, 10 were retired and 18 new commanders were appointed.