IRBIL, Iraq — Karam Kahlid was 18 and working at an orphanage in Mosul when the Islamic State conquered the city more than two years ago.

The militants first burned the orphanage’s books, except the Qurans. Then they beat the children and, finally, indoctrinated them into extremism.

“The children were so afraid and very obedient,” said Kahlid, who escaped Mosul in June and now lives in a refugee camp in the semi-autonomous Iraqi province of Kurdistan.

“The Islamic State forced them to watch their violence, trained those older than 8 to use weapons. They forced them to wear Afghani outfits. I was obliged to shorten my pants, not to shave and keep away from television,” added Kahlid, who was raised in the orphanage where he worked.

He said he tried to counter the Islamic State’s fighters influence over the children. He told them that the militants killed other children’s mother and fathers. They didn’t like being orphans, so they shouldn’t doom other kids to the same fate, he stressed.

“I told them, ‘You have to remember when you first came to this place, you were lonely and broken. You know what a tragedy means. Do not let others feel like that,’” he said. “I told them that injustice is the worst thing ever.”

Despite Khalid’s pleas, many of the orphans became child soldiers. Now, as Iraqi forces surround Mosul, and U.S.-led coalition airstrikes pound entrenched Islamic State positions in Iraq’s second-largest city, Khalid fears that his former wards and other child soldiers will die in an imminent bloody fight that could last weeks or longer.

At least 300 children have died in fighting around Mosul, according to the British-based Syrian Observatory for Human Rights, a watchdog group. The children were part of what the Islamic State called “Caliphate Cubs.”

The Islamic State, also known as ISIL or ISIS, has pressed Iraqi children into military service since the militants seized Mosul and other parts of Iraq in 2014. The terror group has showcased children in many gruesome propaganda videos aimed at Western audiences. They show children executing alleged spies and driving vehicles packed with explosives on Iraqi and Kurdish fortifications.

Last year, the United Nations documented how Islamic State militants burst into high schools in the Hay al-Tamin region of eastern Mosul and abducted youths who were sent to military training camps in Tal Afar in western Iraq and Raqqa, the group’s de facto capital in Syria.

The militants use children to bolster the ranks of their fighters and also to season a new generation of extremists, according to a report by Israel-based International Institute for Counter-Terrorism.

Those who have worked with these children still worry about them after they are freed from the militants.

“The children rescued from (the Islamic State) were highly influenced by them or severely suffering from psychological trauma," said Abu Yousif, 38, an Irbil-based psychotherapist who was displaced from Mosul and has worked with children recruited and brainwashed by the militants.

"That is because they have witnessed killing, lashing, torture and rape," he said. "The children I worked with were so broken and hostile at the same time. ... These children are like a time bomb in society," he said.

Abu Omer, an Iraqi intelligence official, said Iraqi forces were already prepared for the grim task of fighting Caliphate Cubs.

The Islamic State "has been working hard to create a generation of young boys to serve as suicide bombers,” he said. "They will push them to blow themselves up to defend Mosul.”

The children were a sign of desperation as the Islamic State’s numbers dwindle under a multinational onslaught from Libya to Syria to Iraq, Omer said.

"In the past they were counting mostly on foreign militants for suicide attacks,” he said. “Now it's time for the generation they created (to take over).”

The situation for children in Syria is probably even worse than in Iraq.

The observatory estimated that the Islamic State was using thousands of soldiers in Syria, including 4,000 in Deir Ezzor in the eastern part of the country. Those numbers might even be low. The U.N. noted that about 700,000 children, many of them orphans, are among the refugees within Syria because of the country’s 5-year-old civil war.

As Iraqis wait and see how their government’s progress against the Islamic State in Mosul will proceed, Kahlid in Irbil lives with the guilt of leaving many of his former charges behind.

He says memories of the night when the Islamic State first entered Mosul keep running through his mind. The Iraqi army had fled. The other orphanage caretakers decided to follow them. Khalid and a friend stayed.

“We decided not to leave the children alone. We took care of them with great love,” Khalid said.

He fled Mosul only after Islamic State fighters flogged him for refusing to join their cause. Today, he works for a local foundation that provides humanitarian assistance to refugees. He hopes that someday he might help some of the children he once cared for in Mosul.

“I want the war to end so I can go back to the orphanage — I miss it and the children," he said. "Until then, I look at their photos in an album we made so as not to forget their faces. … And I see them in my dreams every night."

Nabeel reported from Istanbul.