MOSUL, Iraq — The collapse of the Islamic State in its most important Iraqi strongholds has brought a rare moment of hope for a country mired in war for most of the past four decades.

It is also a moment of peril, as Iraq emerges from the fight against the militants only to be confronted with the same problems that fueled their spectacular rise in 2014.

Old disputes between Sunnis, Shiites and Kurds over territory, resources and power already are resurfacing as the victors of the battles compete to control liberated areas or jostle for political advantage in the post-Islamic State landscape.

These rivalries now are compounded by the mammoth task of rebuilding the towns and cities destroyed by the fighting, returning millions of displaced people to their homes, and reconciling the communities that once welcomed the Islamic State’s brutal rule as preferable to their own government’s neglect and abuse.

A failure to manage the post-conflict situation risks a repeat of the cycle of grievance and revolt that fueled the original Iraqi insurgency in 2003, and its reincarnation in the form of the Islamic State after 2011, Iraqis and other observers say.

But it is a vast and potentially insurmountable challenge, laid bare in the traumatized communities of Mosul. In the city’s relatively unscathed east, life has bounced back. Traffic clogs the streets, music blares from markets and stores are piled high with consumer goods, such as cellphones, air conditioners and satellite dishes, that were banned or hard to find under Islamic State rule.

In the ravaged west, which bore the brunt of the fighting, entire neighborhoods have been leveled beyond repair. In the Old City alone, 230,000 people have been left without habitation, and “they are not going home soon; the whole district has to be rebuilt,” said Lise Grande, the deputy special representative of the U.N. mission in Iraq.

So far, there is no sign of any reconstruction effort on the scale that will be required, said Hoshyar Zebari, a former Iraqi foreign minister who is from Mosul and now works as an adviser with the Kurdish regional government.

“All the writing is on the wall that there will be another ISIS,” he said, using an acronym for the Islamic State. “The scale of frustration. The lack of hope. The lack of government stepping in. What can you expect?”

Meanwhile, distractions loom as Iraq’s focus shifts to the long-standing political rivalries that were put on hold by the imperative of confronting the Islamic State.

The Kurdish region is pressing ahead with a referendum on independence — over the strenuous objections of Iran, Turkey and the United States — that has the potential to ignite a new war before the present one is over. The vote is reopening the contentious question of where the borders of the Kurdistan region lie, and tensions are rising in areas where the Kurdish peshmerga forces and Iranian-backed Shiite militias have been brought face-to-face by the war against the Islamic State.

Rifts are emerging within Iraq’s governing Shiite majority, which rallied behind the country’s security forces and militias — known as al-Hashd al-Shaabi, or the popular mobilization units — for the sake of fighting the Islamic State. There are sharp divergences, however, over the future identity of the country, over whether it should tilt further toward Iran or maintain an alliance with the United States, and over how far to go to reconcile minority Sunnis with the Shiites.

These issues are expected to come to the fore in elections due in the spring that could become a focus for conflict as the political parties behind the Iranian-backed militias that played a big role in the fighting seek to capitalize on their victories by winning a bigger share in parliament.

The country’s Sunnis are in disarray, scattered among refugee camps or returning to wrecked homes in towns and cities that have been laid waste. Some 2 million of the 5 million people displaced by the fighting over the past three years have returned home. But 3.2 million still live as refugees, mainly in dismal camps, according to the United Nations. Many have no homes to which they can return, and others fear retribution from neighbors or the security forces, Grande said.

In Mosul, there is relief that the militants have gone but also trepidation about what the future holds. Multiple militias roam the streets, loyal to a variety of political masters, government ministers, tribal leaders and members of parliament. The government security forces are spread thin, and some have been withdrawn and deployed elsewhere for the other battles still to be fought before the final territorial defeat of the militants.

Some of the armed men in ­Mosul are local Sunnis, trained as part of a U.S.-promoted initiative to include locals in the city’s future security arrangements. Others are members of the Iranian-backed Shiite militias that were kept out of the battle for fear they would inflame sectarian tensions, but that have moved in to set up offices and recruit local allies.

The militias are needed because there are not enough police and other security forces personnel to keep the city safe, said Mohammed al-Sayyab, a businessman originally from the majority-Shiite city of Basra who heads a small Sunni fighting force controlled by the minister of education. “We cannot say it is 100 percent safe. It is 70 percent safe,” he said. “There are still ISIS sleeper cells. We are working to clear them, but we are up against a very clever enemy.”

Few think the Islamic State has gone away. Everyone, it seems, has a story about someone they know who was with the militants and has reappeared in their neighborhoods, sometimes after being detained and freed. Corruption within the security forces and judiciary contributes to the perception that Islamic State fighters have bought their way out of prison.

Omran Mohammed Bashir, 32, who runs a laundry in eastern Mosul, ticked off on his fingers the former Islamic State members he has seen around his area and elsewhere in the city. Among them are a relative who has not been detained, even though her father reported her to the security services, and a man who commanded the fighters in Bashir’s neighborhood; Bashir ran into the man while visiting a different part of Mosul.

“I don’t think there will be any support for another insurgency. The people of Mosul have learned a lesson,” he said. “But it’s unpredictable what will happen, especially if the situation continues like this, with no reconstruction and corruption inside the government.”

But Iraq has no budget for reconstruction, government officials say. Years of declining oil prices and the financial demands of the war against the Islamic State have left the country bankrupt, forced last year to take a bailout from the International Monetary Fund.

The absence of a discernible reconstruction plan in turn fuels perceptions among Sunnis that the Shiite-led government is neglecting them, said Hassan Alaf, the deputy governor of Nineveh, the province in which Mosul lies.

“It seems some of the politicians are not keen to bring life back to Mosul,” he said. “We still suffer from sectarian conflict, and its implications are reflected in the reconstruction.”

It will be left to the international community to come up with the money to repair the damage, much of it caused by the relentless airstrikes and artillery bombardments conducted under the auspices of the U.S.-led coalition formed to fight the Islamic State, according to Grande, the U.N. representative. The United Nations is planning a fundraising conference in Kuwait this month at which it will seek up to $100 billion in donations for Iraqi reconstruction.

But the countries that enthusiastically prosecuted the war are proving less willing to pay to fix the resulting damage, U.N. and aid agency officials say. The U.S. military has spent $14.3 billion on fighting the Islamic State in Iraq and Syria over the past three years, according to Pentagon figures, but just 10 percent of that — or $1.4 billion — on repairs.

The State Department has asked for $300 million to fund basic repairs such as fixing electricity and water systems in 2018, but the United States does not plan to contribute to the reconstruction effort. The U.S.-led military coalition “is not in the business of nation-building or reconstruction,” Secretary of State Rex Tillerson said earlier this year.

One glimmer of hope lies in a recent rapprochement between the Iraqi government and Saudi Arabia, which have been icily estranged since the 2003 U.S.-led invasion brought a Shiite-dominated government to power in Baghdad. Iraqi Prime Minister Haider al-Abadi has visited the kingdom, and so has the Iraqi Shiite cleric Moqtada al-Sadr, who has broken ranks with Iran’s Shiite allies in Iraq to champion calls for reconciliation with Sunnis.

U.S. and U.N. officials hope the wealthy Arab states of the Persian Gulf will provide much of the funding. But they are embroiled in their own conflicts, disputes and budget shortfalls, and may not have the will or inclination to come up with the many billions of dollars required.