Islamic State gunmen launched a brazen raid Friday on the northern city of Kirkuk, attacking government buildings, hotels and police positions in what appeared to be an attempt to divert resources and attention from an Iraqi offensive on northern city of Mosul, where the group is losing ground.

Running gun battles continued in Kirkuk for much of the day after the militants attacked in the early morning hours. By sundown, officials said, most of the militants, who were armed with grenades andsuicide vests, were dead. A curfew was imposed in the city, and Friday prayers were canceled.

The Kirkuk provincial governor, Najmiddin Karim, said Kurdish helicopters were called in to carry out a strike in the city, which has a population of nearly 1.5 million. In a statement, Prime Minister Haider al-Abadi said he had ordered additional troops to the city.

The assault on oil-rich Kirkuk comes just days after Iraqi and Kurdish forces launched a large-scale battle to retake Mosul, the biggest urban center controlled by the Islamic State group and about 100 miles to the northwest. Islamic State militants have often lashed out when they have lost territory in the past, including by carrying out bombings in the capital, Baghdad.

Security officials say they expect such incidents to increase as the group comes under escalating pressure.

“They are trying to cause confusion and chaos,” Karim said. “They know they'd never be able to control anything here, and they came ready to die. Once in awhile when they get defeated, they try to show some strength somewhere. Every Iraqi city is vulnerable.”

Between 20 and 30 militants carried out the attack, according to the governor.

The heaviest fighting took place between 3:15 a.m. and around 8:30 a.m., said Lt. Col. Sarhad Qadir, a local police chief. The militants attempted to take control of the old police headquarters in Kirkuk using two suicide bombers and attacked a local government building, a political party office and multiple positions of security forces. They also took over a local mosque and urged worshipers to fight the security forces, local officials said. Civilians picked up arms in an attempt to repel the attack, they said.

The Islamic State fighters later holed several hotels, positioning snipers on roofs and taking hostages. Lt. Col. Abdullah Majid, a security chief, said militants wearing police uniforms and using stolen police vehicles also set up a checkpoint in a southern neighborhood of the city, kidnapping at least 10 people who were mostly local security forces.

Qadir said the situation was “under control” by nightfall, though the Kurdish television channel Rudaw said the hostage situation in one hotel continued, with three families still held.

It was not immediately clear how many people were killed in Kirkuk, but at least 13 died in an attack on a power plant and gas station in the district of Dibis in northwest Kirkuk, according to the Electricity Ministry. It said eight of its workers and five Iranian contractors were killed in the attack. Iran’s official Islamic Republic News Agency said four Iranians were killed and three wounded. A journalist for a local television channel was killed by a sniper.

The complex military battle to retake Mosul from the Islamic State has begun. Here is what you need to know about the ancient Iraqi city. (Ishaan Tharoor, Kareem Fahim, Jason Aldag/The Washington Post)

“It’s a desperate attempt to move the front to Kirkuk and give their people who are besieged in Mosul a chance to escape,” said Ammar Kahiya, a member of Kirkuk's provincial council. “The militants attacked the provincial council building but did not manage to control it. They managed to enter the police station but had lost it by midmorning.”

Hassan Touran, a lawmaker from Kirkuk, said the attack was launched by Islamic State “sleeper cells.” Karim, however, said he believed the militants had "infiltrated" the city.

Col. John Dorrian, a spokesman for the U.S. military in Baghdad, said he could not confirm reports that coalition jets had also carried out strikes to put an end to the assault, or that one strike had missed its target and hit a funeral.

On Friday, the militants claimed to have repelled multiple attacks by Kurdish forces, including around the town of Bashiqa. In a statement, the Islamic State said it launched 18 suicide attacks.

The peshmerga forces opened new fronts Thursday against the militants in Mosul, with Iraq’s elite counterterrorism forces joining the fight for the first time. Security forces made some advances but were met with tough resistance.

On the newly opened northern front, Kurdish forces said that “a number” of peshmerga “paid the ultimate sacrifice” during their offensive, adding that air support from a U.S.-led coalition had not been “as decisive as in the past.”

The counterterrorism units faced a barrage of 15 car bombs when they launched their assault on the town of Bartella, six miles east of the city of Mosul.

A U.S. defense official, speaking on the condition of anonymity to discuss an ongoing situation, said a U.S. service member who was killed Thursday had been operating with Iraq’s elite counterterrorism service and was riding in a vehicle when it struck a roadside bomb. The official said the U.S. service member was the only fatality from the blast.

On his way to visit his counterpart in Turkey, Secretary of Defense Ashton B. Carter said the slain American had been serving in an advisory role. Carter did not elaborate on the circumstances of his death.

“It’s a reminder that our people who are participating in the counter-ISIL campaign . . . are in harm’s way,” Carter said, using an acronym for the Islamic State.

As Iraqi, Kurdish, and coalition forces continue to press toward Mosul, Carter voiced concern about efforts to stabilize the city after the Islamic State is pushed out. He said he hopes the reconstruction of the city will “not lag behind the military effort.”

“I'm encouraged by the progress so far, but this is going to be a serious military campaign, and it’s in the very earliest stages.”

Aaso Ameen Shwan in Irbil and Thomas Gibbons-Neff in Ankara contributed to this report.