Mosul, Iraq - Doctor Omar was on duty in Mosul's eastern al-Khansa hospital when the Islamic State of Iraq and the Levant (ISIL, also known as ISIS) stormed the city in early June 2014. The slim 37-year-old saw the flames in the distance as fighters torched government offices and heard confused reports from friends and relatives of masked gunmen on the streets. Colleagues began to panic, to talk of leaving town. But Omar, who asked not to be referred to by his full name for fear of retaliation, had patients to treat, so he continued his rounds of the paediatrics department.

The Iraqi troops and police deployed in the city collapsed almost immediately and many of Mosul's residents awoke to find themselves under ISIL's control. Omar carried on working, filling gaps in the rota where other staff had fled. A few days later, a local man called the hospital. He told them that he was with ISIL and had removed local health ministry officials. From now on, he added, the doctors answered to him.

Across Mosul's largely shuttered public service buildings, the same process repeated itself, more than a dozen civil servants told Al Jazeera. The group installed their own people at the top, then located street cleaners, electricity department personnel or teachers and told them they would be killed if they did not come back to work.

This was a systematic process designed to give ISIL's self-declared caliphate the trappings of nationhood beyond sheer military might. Government workers were made often-unwilling participants in ISIL's so-called Islamic State. They helped the group maintain a hold on Iraq's second city that was finally ended this month by what US military commanders have described as the largest urban military operation since WWII.

READ MORE: What will post-ISIL Mosul look like?

Some employees did not have to be threatened. To Omar's surprise, three al-Khansa staff sided with the new rulers. He recalled seeing one, a nurse named Mohammed, in the corridor. The man was burlier and had grown a long beard, so Omar tried unsuccessfully to avoid his gaze. "He said 'Hello doctor; I joined ISIS.' I didn't know what to say."

The government in Baghdad continued to pay salaries to state workers - even as security forces battled the fighters elsewhere - couriering vast sums of cash into the city. Political leaders hoped this would prevent civilian suffering. But it also ensured that institutions continued to function under ISIL rule and allowed the group to skim funds for themselves, adding to substantial income from oil and taxation.

Speaking at his east Mosul home, Omar explained that initially "ISIL was lenient." They even let him travel to Jordan in November 2014 to sit postgraduate clinical exams, though they warned that if he did not return within 30 days, his family home would be confiscated.

The journey involved a six-hour taxi ride and a three-mile walk in knee-deep mud. But leaving was like going "from hell to heaven", he said while showing a picture of himself smiling on the morning of his exam and another of him and his friends relaxing by the Dead Sea. A few days later he was back in Mosul.

Schools had reopened for the fall term by then, and ISIL had mandated changes in line with its strict interpretation of Islamic law, according to a 46-year-old primary school teacher who asked to go by the pseudonym Abu Ahmed.

Discussion of the Iraqi government and its borders was banned along with pro-science slogans. Photographs of the opposite gender were to be censored from textbooks - teachers instructed students to go through and scribble them out themselves - hair styling was prohibited, and sports jerseys were only allowed if the badges were ripped off.

Despite ISIL's attempt to portray Mosul as an Islamic utopia, services were distributed in an ever more unequal manner. Two squat, short-haired men who worked with the electricity department, said they and colleagues were instructed to concentrate on supplying power to ISIL fighters at all times. The rest of the city had six hours a day or less. "Electricity was 24/7 for ISIL fighters and ISIL bases, and the foreign fighters were most important," one said, referring to members who had come from outside Iraq.

Mosul residents said repeatedly that while life under ISIL was difficult, the city was unusually clean. This was thanks to the toil of street sweepers forced to work harder than ever. After a day's work in a battle-damaged eastern district, 21-year-old Hatim Jassim described how ISIL had increased their shifts by two hours and cancelled their day off.

Here too, the fighters benefited disproportionately. "The places that their bosses lived had to be cleaned every day," Jassim said, adding that ISIL fighters also confiscated their bright orange overalls. Jassim and his colleagues could not understand why until they saw them on victims in one of ISIL's notorious execution videos.

READ MORE: Mosul in ruins: 'I see only despair around us'

By the following year, ISIL had consolidated its hold on the city, the jewel in over 38,000 square miles of territory. As they did, the coalition of forces assembled by the US in 2014 to battle the group began to increase support operations, helping Iraqi and Kurdish forces to claw back ground.

Inside Mosul, conditions became stricter. Smoking was prohibited, mobile phones were confiscated along with satellite dishes and televisions. Failure to comply meant fines, jail, or physical punishment, with much of the cruellest treatment meted out by foreign fighters.

At the hospital, Omar treated the children of fighters from countries that ranged from China, Somalia and Tunisia to the United States and Brazil. The parents were often angry or anxious, and he was terrified of what they might do if something went wrong.

In July 2015, Baghdad stopped paying government salaries. ISIL began to issue public workers meagre wages of around $70 a month - with the additional incentive of death for anyone who refused.

When school terms started, ISIL fighters collected textbooks and burned them, then distributed CDs of replacements to be printed off at the students' expense. Abu Ahmed showed a set; reams of stapled A4 sheets marked with faded ink.

The changes were vast this time. History related only to ISIL and its precursor organisations, geography became the borders of ISIL territory. Even maths examples were changed to include guns and bullets. "They said not to mention Pythagoras's name because he's a disbeliever," Abu Ahmed said with an incredulous laugh.

Asked if he felt bad for perpetuating ISIL ideology, the huge, heavy jowled teacher nodded vehemently. "A child's mind is just like wheat before you make bread, you can do whatever you want."

But many of his students were the children of fighters, and he feared them, especially after a colleague was lashed for ignoring the new curriculum. "Of course, I was scared… I was worried that they might lie about me if they disliked me, I'd change my behaviour around the children in case they said anything [to their parents]."

Instead, he began to use the pretext of regular family gatherings to teach young relatives maths, reading, and even basic science. He swore the children to absolute secrecy every class, well aware of the likely punishment if he was caught.

Meanwhile, ISIL was discovering that Mosul would not run on fear alone. The electricity workers were short on materials, so ISIL fighters looted supplies from abandoned Christian towns outside the city.

In some cases, they forced other city employees to do the labour. "We became thieves," Jassim, the young street cleaner said. "They'd order us to 'clean' a district, but it wouldn't be cleaning, it would be stealing."

The group became more anxious about controlling these workers, recruiting informants to report infractions like smoking or watching television and cruelly punishing disobedience.

One day in 2015, an 18-year-old street cleaner asked his ISIL supervisors for time off after his father died, Jassim said. But his request was turned down and in an ensuing argument one of the fighters - another young man from Mosul - shot him dead.

READ MORE: ISIL families lament harsh treatment in Iraq

By mid-2016, ISIL had little more than fear to fall back on. Iraqi and Kurdish security forces encircled Mosul and conditions inside the city worsened, with food prices soaring. Doctors in al-Khansa began to see more and more preventable diseases, as well as cases of malnutrition. When the offensive finally began in October, ISIL fighters would burst into the hospital with wounded comrades and demand they be treated before civilians.

Their rules became stricter still and enforcement unreasoning. Omar remembers an ISIL fighter screaming at a mother who lifted her niqab to comfort a child who was having an IV drip inserted.

The army reached Omar's northeastern district in November 2016 and announced an all clear after six days of fighting. The doctor walked outside hesitantly and was immediately shot in the thigh. It was a flesh wound, but there were was no medical facility nearby, so he took a scalpel, dug out bullet fragments without anaesthetic then sutured his leg back up.

A week later, he limped to the primary healthcare facility two kilometres away and began to work again. The east of Mosul was declared officially retaken in January as devastating fighting continued on the other side of the Tigris. Reconstruction began soon after, and trucks laden with building materials and food soon tailed back at checkpoints into the city each morning

But many state workers said they had still not been paid. At a number of hospitals, many of which are still badly damaged, doctors complained they only received supplies from NGOs, while volunteer medics said it felt as if their government had abandoned them.

Some schools, meanwhile, were still rubble and without books and materials, teachers worried about the coming fall term. Abu Ahmed and his colleagues staged campaigns on social media and petitioned local officials for help.

But back home on his day off, Omar was optimistic. "We are amazed at ourselves," he said with a smile. "We withstood ISIL; we can withstand this."