HAMMAM AL-ALIL, Iraq — The UN refugee camp near this small town just south of Mosul is a sun-baked sea of white tents. In one tent, 15-year-old Atallah Saleh swats at flies and looks at the ground shyly. His sweet smile disappears as he describes the three years he spent under ISIS rule.

"When Daesh came, they taught us how to be suicide bombers and make IEDs," he says, his eyes glistening with tears. "They distributed books about their propaganda. The teachers at school taught us how to hold a Kalashnikov, how to shoot and kill, how to become a suicide bomber and fight the jihad."

From a military standpoint, ISIS, or Daesh as it is known here, looks like it might be on its way out of Iraq. Some in the media are already calling the fall of Mosul the end of the terror group in the country.

But ISIS came to Iraq not just to conquer, but to settle. In every territory under its control, the group took over schools and mosques, installing radical imams and teachers to inject its ideology into a new generation. As ISIS loses ground militarily, the civilians it spent years indoctrinating are now scattered across the country in refugee camps.

Some camps are reported to have inhumane living conditions. Others are plagued by retaliatory violence against their Sunni Muslim residents. All provide a potential incubator for terror, as youths like Atallah who have already absorbed the radical Islamist mentality of ISIS begin to nurture resentment against their liberators.

Related: ISIS Militants Live Among Liberated Civilians in Mosul

ISIS was born from the DNA of a group that was once thought to be defeated, Al Qaeda in Iraq. Local leaders like Qassem Maslah, a brigade commander in a militia that has been fighting ISIS in Iraq, the Hashd al-Shaabi, worry that that ISIS could experience a similar resurgence in a few years via the thousands of youths it has indoctrinated.

Atallah Saleh, 15, stands inside a tent with his siblings at the Hammam al-Alil refugee camp in northern Iraq. Sulome Anderson / for NBC News

Let our news meet your inbox. The news and stories that matters, delivered weekday mornings. This site is protected by recaptcha

"When a chicken lays eggs, and then the chicken dies, the eggs stay and turn into new chickens," says Maslah. "These groups have different names, but they are all the same."

"[W]hen Daesh came to this area, to these villages — these are poor people. They herd animals; they are not educated. So Daesh built the Salafist, takfiri ideology into them. They were training children from six, seven years old, to hold weapons and kill people. What we need is to reeducate them in schools, and teachers should show them how to get rid of these ideas, because they are children and these things stay in their minds."

Related: ISIS Revenue Falls 80 Percent as Militants Lose Ground

Zaid Adil Sultan, the Hammam al-Alil camp manager, says the ISIS school curriculum was intended to keep the jihadi ideology alive among the young even if the group was defeated.

"They gave them 'courses' that encouraged violence and taught them the concept and ideology of jihad," he says. "In math, instead of teaching them that one plus one equals two, they taught them that one bullet plus one bullet equals two bullets. They opened workshops to prepare [boys] to fight, show them how to build muscle, things like that… they put young boys in mosques and gave them lectures on Islam and how to be a true jihadi."

Zaid Adil Sultan teaches a class for displaced Iraqi children in a tent school organized by the UNICEF at the Hammam al-Alil camp south of Mosul, Iraq on May 23, 2017. Ari Jalal / Reuters file

Sultan works at the camp's school, at the center of the sea of bleached tents, where teachers try to undo the influence of ISIS. Sultan says those kids who were forced to attend ISIS schools were deeply traumatized by the experience.

"After we opened [the] camp and children started to come in, we saw that their psychological situation was a disaster," Sultan explains. "They were afraid of noises, planes — you cannot describe how bad their state of mind was. But we've opened workshops to help them repair their psychological problems. We have a psychologist and psychiatrist who specialize in this kind of trauma treatment. We try to teach them that Daesh is gone and they shouldn't be afraid…we opened courses on true Islam and how they should live as Muslims."

Related: ISIS Leader Who Approved Sex Slaves Killed By U.S. Airstrike

But according to Sultan, these attempts at reprogramming don't always stick. "The hardest age to treat is boys from 14 years old to 17," he says. "People have told me that before their sons went to those schools, they were okay, but after they went, they were coming home, hitting their siblings and threatening to kill them."

The "mentality," said Sultan, is "very difficult to eradicate. We are working hard, but the circumstances are not good for making sure Daesh doesn't come back as a different group."

Saleh Saleh sits with his family outside a tent at the Hammam al-Alil refugee camp in northern Iraq. Sulome Anderson / for NBC News

Atallah's father, Saleh Saleh, a regal, elderly sheikh with three wives and many children, holds court in the family tent, which simmers in the midday heat. He was troubled by how his kids behaved after absorbing the ISIS brand of education.

"When the children came home, I would tell them not to listen to anything they heard from Daesh, that what they were telling the kids was all lies," says Saleh. "But they are children, and they sometimes would repeat things without understanding them. That is what they were seeing and hearing every day."

Saleh says he knows many boys around Atallah's age who were unable to let go of the ISIS mentality, even after they were liberated and moved to camps.

"I saw many kids who followed ISIS after they went to these schools and eventually became Daesh," he says. "Since the NGOs came to work in the camp and reeducate the children, it's gotten better. But before they came, when the camps were first established, there were many children like that."

One of Saleh's wives, a young Bedouin, beckons and points to a tent just across the street. "That woman's husband was just arrested for being Daesh two days ago," she says quietly. "They took him away and put him in prison. She has a son Atallah's age."

Young Atallah Saleh says the brutal ideology of ISIS doesn't hold any appeal for him. But it's clear he won't be recovering from the group's education anytime soon.

"I see the men who taught us in school in my dreams," he says. "I can see their beards and their eyes. Everything about them was frightening."