Troops in Iraq have claimed victory over Islamic State militants in a key oil hub in the country’s north, but a deadly suicide bombing targeting government forces and continued clashes with the extremists have underscored the fragility of such gains.

Iraqi police and soldiers backed by pro-government militias entered Baiji on Tuesday, officials said, for the first time since the Islamic State seized the industrial city as part of its lightning offensive across northern Iraq in June. Security forces recaptured three buildings in central Baiji, including the local council and police headquarters as well as a mosque, officials said.

Security officials said the takeover marked the beginning of a wider operation to also break the militants’ months-long siege of a nearby oil refinery — the country’s largest. Baiji is about 140 miles by road north of Baghdad.

But later Tuesday, a suicide bombing killed as many as six soldiers in the same area that government troops claimed to have liberated from the militants. Police Brig. Gen. Khalil Ramal Ahmed said a jihadist lured security forces into an abandoned house before detonating his explosives.

“There are still clashes near the police headquarters” in the center of the city, Brig. Gen. Maan al-Saadi, the head of Iraq’s counterterrorism forces in Salahuddin province, where Baiji is located, said Wednesday afternoon.

Ahmed said government forces have faced suicide attacks, car bombs, snipers and improvised explosive devices in the wake of their push into central Baiji. On Wednesday, security forces defused more than 30 IEDs in the center of the city, he said. Iraqi state television aired footage of sandbagged armored vehicles crawling through deserted streets lined with the signature black flag of the Islamic State, an al-Qaeda offshoot that is also known as ISIS or ISIL.

“It is not safe for people to come home,” Ahmed said. “It is still a battlefield.”

If retaken by government forces, Baiji, with a population of about 70,000, would be the largest city wrested back from the Islamic State. The group controls wide swaths of territory in Iraq and Syria, including Mosul, the largest city in northern Iraq, and the mid-size city of Tikrit.

A complete takeover of Baiji by Iraqi troops would cut off supply lines to Mosul, which lies about 115 miles north by road.

The Islamic State’s fighters also have besieged the Baiji oil refinery on the outskirts of the city since June, trapping Iraqi security forces inside.

The troops have held out against repeated attempts by the militants to seize the refinery. But government forces have been unable to infiltrate Islamic State reinforcements around the facility.

A spokesman for Asaib Ahl al-Haq, a powerful Shiite militia aligned with the government, said some of its forces were with Iraqi troops inside the refinery. He said Islamic State fighters have surrounded the refinery with mortars. In Baiji last month, the militants downed two Iraqi helicopters with surface-to-air missiles.

A month ago, U.S. forces airdropped food, water and ammunition to the Iraqi troops inside. Coalition forces also have carried out 18 airstrikes in or around Baiji since Nov. 1, according to the U.S. Central Command.

“We will liberate all of Baiji,” Ahmed said. “And, God willing, we will reach the refinery in the coming days.”

Mustafa Salim contributed to this report.