The United Nations estimates more than 3.3 million Iraqis have been forced from their homes by violence since 2014

The skies above this small northern Iraqi town are black with smoke and ash rains down from around a half dozen oil wells that Islamic State group (I.S.) fighters set ablaze as Iraqi troops moved in to retake Qayara last week.

The apocalyptic scene underscores the sort of destruction that the militants are likely to wreak as Iraqi forces move toward Mosul, the biggest prize still held by I.S. in Iraq.

Unlike previous ground assaults against I.S. in Iraq that left entire cities and villages emptied of civilians, thousands of civilians remained in Qayara as militants inside quickly folded up and fled, a sign of their weakening morale and damaged supply lines, commanders say. That means residents did not join the ranks of hundreds of thousands of people displaced by recent fighting with I.S. and now languishing in camps around the country.

But the situation for the some 9,000 civilians still in Qaraya is precarious. The battle left the town without electricity and little running water, and the large international aid groups who normally help the displaced say they cannot deliver aid to people so close to frontline fighting.

Najim al-Jobori, the commander of military operations in Nineveh Province where Qayara and Mosul are located, said Iraqi forces are increasingly trying to keep civilians in place while pushing I.S. fighters out. The Qayara operation, he said, raises his hopes that the approach on Mosul will become increasingly easier as morale among I.S. fighters crumbles

He said that previously when an airstrike hit an I.S. unit, the survivors would stay and keep fighting. “But now, you never see that anymore, they all just run.”

Hundreds of civilians poured out into Qayara’s main street Sunday as a convoy of Iraqi officials pulled into the town just days after the military retook it. Some children rushed to cheer on the Iraqi army Humvees, other families peered cautiously from behind garden gates in the town, which before 2011 had a population of 79,000.

Walls still painted with colorful I.S. instructions and warnings were only partially obscured with hasty swipes of paint. I.S. banners at the town’s entrance stood shredded.

Salim Atiyya, a government employee, said that initially in the lead-up to the military’s assault on the town, planes dropped leaflets on his neighborhood telling residents to flee along a road leading west. But I.S. fighters immediately mined the road with roadside bombs. A few days later, leaflets were dropped telling residents to stay put.

“At the beginning of course we were so scared,” the 33-year-old Atiyya said. “We found the smallest room in our house away from windows and doors and we all moved into there,” he said. Extended family members also moved in, seeking safety, and eventually “18 of us were all in that one small room,” he said.

Warplanes from the U.S.-led coalition bombed militants in the town for three days, and then Iraqi forces moved in, recapturing Qayara with only minor clashes. The militants set fire to the oil wells initially to try to thwart airstrikes, but then as they realized they were losing ground they set as many wells alight as possible in an attempt to leave behind a ruined prize, residents said.

After the militants fled, Atiyya said he and his family emerged and found the bodies of three I.S. fighters killed by an airstrike in the street in front of his home.

“We took them and threw them in the garbage dump,” he said, adding, “if you go there now you’ll see it’s filled with the dead from Daesh” — using the Arabic acronym for I.S.

But now al-Jobori says his men are unable to control the oil well fires. It’s an I.S. tactic the Iraqis fear they will continue to face as they close in on Mosul: There are a number of small wells under I.S. control around the city that the militants are likely to set ablaze as well, though they are nowhere near the size of Iraq’s more significant oil fields in the south, the Kurdish region and around Kirkuk.

“The ash falls down like paint powder,” said Atiyya. “You will try to wash your clothing, but the color, it never comes out.”

Another resident, Ahmed Salih, gestured to the clouds of thick black smoke billowing over the town. “The children and the elderly, they are the ones suffering the most,” he said, adding that I.S. began burning oil as a defensive measure three months ago and respiratory problems have since then become commonplace.

Salih’s tire shop stood shuttered along the main drag. Most of the shops on either side were damaged by small arms fire or destroyed by airstrikes or explosives laid by I.S.

The United Nations estimates more than 3.3 million Iraqis have been forced from their homes by violence since 2014. The government and aid groups are already struggling to help the displaced, and now they are gearing up to deal with an estimated 1 million who could be driven from their homes during to campaign to retake Mosul.

But keeping residents in their homes raises problems of its own. The U.N. refugee agency said it has been unable to reach Qayara to assess the situation there because it remains so close to fighting.

“We are concerned that if people are prevented from leaving their homes, it could place them at additional risk,” the agency said in an emailed statement.

On Sunday, Sheik Abdul-Latif al-Himaim, the head of Iraq’s Sunni Endowment, an agency that oversees religious affairs for Sunni Muslims, toured Qayara. He said his agency’s charity arm plans to bring aid to the town as it has to other displaced Iraqis.

But just a few kilometers down the road, there’s still no sign of aid from the endowment to a dusty camp called Teena, which houses hundreds of people displaced by previous fighting in the area.

Unlike previous ground assaults against I.S. in Iraq that left entire cities and villages emptied of civilians, thousands of civilians remained in Qayara. Photo: AP

“Honestly, the situation there is miserable,” said Iraqi Army Col. Karim Rodan Salim, stationed at nearby Qayara air base. “None of the aid groups can reach here easily, any supplies brought there are finished in two days.”

Iraqi forces retook Qayara air base more than a month ago. Now, they are building it up to accommodate the massive numbers of troops needed to push further north toward Mosul.

On Monday, dozens of trucks carrying military equipment, blast walls and vehicles clogged the roads leading to the base. As a convoy of coalition armored vehicles passed, carrying munitions and cartons of Gatorade, children from the Teena camp swarmed the sides of the road to beg, yelling, “Water!” in English.