The Islamic State group swept into northern Iraq in 2014. But the door opened for it three years earlier, when the U.S. withdrew its troops.

Left on their own, Iraqi troops dropped their guns and fled the onslaught. Islamic State militants then ruled the northern city of Mosul and other towns with nihilistic brutality that entailed public beheadings and sex slavery. It took three years for Iraqi troops to regroup, and with the help of U.S. advisers and airstrikes, retake Mosul and the rest of Islamic State-held Iraq.

Today, Mosul slowly rebuilds. Remnants of the Islamic State group have scattered into the desert. Game over? Hardly.

Yes, Islamic State has been vanquished, but Iraq remains fertile ground for a comeback.

The U.S. and Baghdad are stepping up talks about maintaining a U.S. military presence in the country, USA Today recently reported. It’s not known how large an American contingent would be involved, but its role likely would continue to be advising Iraqi commanders and providing surveillance and intelligence.

Keeping American boots on the ground in a part of the world as unstable as Iraq is never an easy decision, but it behooves both Iraq and the U.S. to hammer out a deal.

Iraq’s peace is fragile. Shiite-led Baghdad continues to marginalize the country’s Sunni minority, leaving the nation vulnerable to a resurgence of Sunni-led militancy when the time and circumstances are ripe.

And Islamic State doesn’t need a caliphate to maintain its online appeal to lone wolves, such as the Uzbek immigrant who killed eight people in New York in a truck attack on Halloween, or the Bangladeshi who tried to blow himself up in a crowded New York subway station Dec. 11.

An American military intelligence presence is needed in Iraq to ferret out whatever Islamic State is up to, whether that be web propaganda or suicide bomb attacks in Baghdad.

Just as important, a continued American military presence in Iraq, even if smaller than the force there now, also serves as a counterpoint to Iran, which strives to make Iraq its proxy state.

Though both Iraq and the U.S. paid a stiff price for the pullout of American troops in 2011, the Obama administration didn’t have much choice. President Barack Obama tried to negotiate a deal to keep a contingent of American soldiers there, but Baghdad refused. Opinions vary on whether Obama might have tried harder. But the country’s parliament balked at giving U.S. troops legal protection from Iraqi courts, a condition Obama was right not to accept. Obama was late, however, in realizing the gravity of the Islamic State threat, at one point shrugging off the terror group as “junior varsity.” He later changed course.

Neither the U.S. nor Iraq can afford to again underestimate Islamic State and its bloodlust. A small, focused contingent of U.S. troops can serve as a firewall.

FROM AN EDITORIAL IN THE CHICAGO TRIBUNE