(Reuters) - It was an awkward coalition riven by political and sectarian differences, facing an elusive, fanatical enemy dug into an urban maze of narrow streets and alleyways. So, could Iraq s government really deliver on its vow to vanquish Islamic State? In the end, the army, Shi ite Muslim paramilitaries and Kurdish Peshmerga fighters mustered rare unity to end Islamic State s reign of terror in Iraq s second city Mosul, seat of the ultra-hardline Sunni insurgents   caliphate . Baghdad s victory in July 2017 after nine months of fighting was the coup de grace for the caliphate and came three years after a jihadist juggernaut seized one third of Iraq. But even with supportive U.S. air strikes, Baghdad s triumph came at a devastating cost for the once-vibrant, multicultural city in northern Iraq and the surrounding region. When Islamic State militants first arrived in Mosul in June 2014 after sweeping aside crumbling Iraqi army units, many Mosul residents initially welcomed them. The militants were Sunni Muslims, like many in Mosul who had accused the forces of then-Shi ite Prime Minister Nuri al-Maliki of widespread sectarian abuses. Islamic State consequently presented itself as Mosul s savior. But as jihadists brandishing AK-47 assault rifles began imposing an Islamist doctrine even more brutal and mediaeval than al Qaeda, its popularity soon faded. Maliki s successor, Haider al-Abadi, had long been seen as an ineffective leader who could not make tough decisions. However, a U.S.-backed campaign against IS in Mosul offered Abadi a chance to emerge as a steely statesman capable of taking on a group that had terrorized a sprawling city with beheadings in public squares while staging deadly attacks in the West. Just smoking one cigarette, an act IS saw as anti-Islamic, earned you dozens of lashes. Children were used as informers. Women in minority communities were turned into sex slaves. But taking back Mosul was never going to be easy. Long before the first shot was fired, Abadi and his advisers and military commanders had to tread cautiously, taking into account sectarian and ethnic sensitivities that could splinter the united front he urgently needed to establish. Iraqi and Kurdish intelligence agencies had recruited informers inside Mosul, from ex-soldiers and army officers to taxi drivers, who would face instant execution if caught. Even if an alliance of convenience was struck, glossing over sectarian splits, Mosul itself posed formidable physical obstacles. Key districts consisted of ancient little streets and alleyways inaccessible to tanks and armored vehicles, and they were so densely populated that U.S.-led coalition air strikes risked heavy civilian casualties. So, street by street, house by house, fighting was unavoidable. Such challenges first popped up in Mosul s hinterland as Kurdish forces slowly advanced against fierce IS resistance. In one village, a single IS sniper hunkered down in a house held up hundreds of Kurdish fighters, the U.S. special forces advising them and 40 of their vehicles. Eventually, his rifle went silent after three air strikes on the house. As pro-government forces inched forward, the United Nations warned of a possible humanitarian disaster and expressed fear that jihadists could seize civilians for use as human shields, and gun down anyone trying to escape. IS fighters   both Iraqis and foreigners - were experts at carrying out suicide bombings and assembling homemade bombs. Many houses were booby-trapped. Iraqi military commanders had to factor these lurking perils into their gameplan. In interviews, IS insurgents shed light on what Iraqi forces were up against. They were quite open about their ideology and what they were willing to do to transform the Middle East. One man said he had used rape as a weapon of war against more than 200 women from Iraqi minorities, and had killed 500 people. After months of grueling fighting, Iraqi forces finally attained the outskirts of Mosul, but any celebrations were premature. Bombs littered dusty roads. Car bombs were exploding. A Mosul resident explained that his child no longer flinched as explosions shook his street because many people, including the young, had grown numb to the daily bloodshed. Each side resorted to desperate measures to gain an edge. In north Mosul, people walked by fly-infested, bloated corpses of militants who had been left on roadsides for two weeks. Iraqi soldiers explained that the stinking bodies had been left there to send a clear message to residents - don t join IS or you will suffer the same fate. Caught in the middle were civilians who had suffered under the IS reign of terror for three years and were now wondering if they would survive a relentless battle to  liberate  them. Parents waited patiently after weeks of fighting for a largely unknowable right moment to make a dash for Iraqi government lines, clutching their children, risking a run-in with jihadists from places as far away as Chechnya. As much of east and west Mosul was pulverized by coalition air strikes or IS truck and car bombs, the city was reduced to row after row of collapsed or gutted housing. In the end, IS suffered its most decisive defeat and watched their self-proclaimed caliphate evaporate in Iraq, then in Syria as Kurdish-led forces retook Raqqa, IS s urban stronghold there. But those victories will be followed by tough questions about the future of both Iraq and Syria. Preserving the shaky understanding forged between the different communities in the run-up to the Mosul campaign will be essential to saving Iraq as a state in the future. It did not take long for the Mosul coalition to fray. In October, Iraqi forces dislodged Kurdish Peshmerga fighters from the oil city of Kirkuk and other disputed areas and Baghdad imposed curbed air travel to and from the semi-autonomous Kurdistan region in retaliation for a Kurdish independence referendum held in northern Iraq in September.   The battle for Raqqa, which became IS s operational base in Syria, had a different feel to it as U.S.-backed Kurds and Arabs in the Syrian Democratic Forces (SDF) tightened their siege. The fighting seemed slower and more measured, step by step along abandoned streets where journalists were given access. In the weeks before Raqqa s fall in October, young female SDF fighters faced off against hardened militants and suffered losses. But that did not curb their enthusiasm and some said they would eventually like to join Kurdish PKK militants in Turkey and help advance their 33-year-old insurgency there. The victors in Iraq and Syria now face new challenges as they rebuild cities shattered by the showdown with IS. After IS s defeat in Raqqa, Raqqa residents formed a council to run the city but they had no budget when it was first set up, just residents streaming into their tin, run-down headquarters demanding everything from instant jobs to getting their damaged farmland back. Syrian Kurdish fighters were inspired by the ideas of Abdullah Ocalan, head of the PKK militants who has been imprisoned in Turkey for almost 20 years. Turkey views the political rise of Syria s Kurds as a threat to its national security and is fiercely opposed to the idea of Kurdish autonomy on its doorstep. The Kurdish groups who led the fight against Islamic State in its former capital Raqqa must now navigate a complex peace to avoid ethnic tension with the city s Arab majority and to secure critical U.S. aid.    So, life for Raqqa s victors will remain fraught with risk. 