The Islamic State has finally fizzled. Its caliphate, daringly declared from the pulpit of the Great Mosque of al-Nuri, in Iraq, in 2014, had been the size of Britain, ruled eight million people, lured recruits from eighty countries, and threatened to redraw the map of the Middle East. It ended, in the Syrian farming hamlet of Baghouz, as little more than a junk yard about the size of Central Park, filled with burnt-out vehicles and dilapidated tents. Tens of thousands of ISIS loyalists, both fighters and their families, opted to surrender—and face life in crammed prisons and dreary detention camps—rather than become martyrs in ISIS’s promised paradise.

The dangers are far from over. ISIS has sleeper agents. “There are thousands,” General Mazloum Kobani Abdi, the reclusive politician turned commander who led the campaign against ISIS in Syria, told me, when I travelled through the former caliphate last month. Abu Bakr al-Baghdadi, the Islamic State’s “caliph” and the world’s most wanted man, has evaded capture. ISIS also still has affiliates active from Nigeria to Afghanistan, from Egypt to the Philippines. “Frankly, our job has just started,” Mazloum warned. “We are finishing the great battle, then we will fight a different kind of war.”

The drive to flush ISIS out of Syrian territory was described as “the most successful unconventional military campaign in history” by the Middle East Institute last month. More than a dozen American diplomats and military officials involved in Syria told me the same thing. The campaign was distinct from the counterpart operation in neighboring Iraq, where the United States coördinated with a friendly government, retrained its conventional Army, provided sophisticated weaponry, established a headquarters for a coalition of seventy-four countries, and had legal status granted by parliament.

The campaign in Syria liberated roughly the same amount of territory. But it relied on an unlikely partnership between élite U.S. Special Forces teams and a scrappy local militia led by Mazloum, a middle-aged Kurdish rebel whose face has been weathered by years of conflict and five stints in Syrian prisons. They all operated in defiance of the Syrian government and its Russian, Iranian, and Hezbollah allies deployed nearby. Their mission was shrouded in secrecy. American troops wore no identifying insignias, ranks, or names on their uniforms. The Syrian fighters knew only the first names of the Americans, including the commander. Yet they established an unusual level of trust. The U.S.-led international coalition provided air cover, but it depended on the Syrian Democratic Forces, or S.D.F., to protect their troops on the ground.

Together, they seized twenty thousand square miles in northern Syria, a dusty region of crops, pastures, and oil fields that reminded me of Oklahoma. When I drove around Raqqa, the former ISIS capital, little girls with ponytails and pastel backpacks were headed to school—after years of being banned from education or even from leaving home. Women, their faces uncovered, strolled to the street markets rising amid the rubble, much of it created by U.S. air strikes. Small groups of men sat curbside, sipping their morning tea and smoking, another practice that was banned under ISIS. At several shops, workers were pounding twisted construction rods—giant metal tumbleweeds pulled from bombed-out buildings—to flatten them, for use in reconstruction. Spring lambs, small and pristine white, nibbled along the roadsides.

In Washington and other Western capitals, the territorial defeat of ISIS, which for years was considered the greatest threat to global security, will almost certainly be studied as a model for future counterterrorism operations. “The S.D.F. is the best unconventional partner force we’ve ever had, anywhere,” Brett McGurk, the former lead coördinator of the campaign against ISIS, who is now a fellow at Stanford, told me. “The S.D.F. in effect conquered one-third of Syria, once the heart of the ISIS caliphate, with very low costs for the United States.” Mazloum, who is on Turkey’s most-wanted list, evolved into one of the most important U.S. allies in the Middle East.

The collaboration—which deepened over three phases—is an epic with twists, tragedies, and, ultimately, betrayal at an enormous cost to America’s allies in Syria. Eleven thousand Kurdish fighters have died since 2014. The Americans have lost eight.

Fighters from the Syrian Democratic Forces staged a parade to celebrate the fall of ISIS in the Deir Ezzor province, which is home to Syria’s most valuable oil fields. Photograph by Delil Souleiman / AFP / Getty

The alliance was seeded in August, 2014, a month after ISIS declared its caliphate and a week after a lightning ISIS offensive began in Iraq. Marauding ISIS fighters murdered thousands of Yazidi men and abducted truckloads of women. Tens of thousands of fleeing Yazidis, a mostly Kurdish-speaking minority summarily declared apostates by ISIS, were stranded on the arid peaks of Mount Sinjar without food, water, or a way out. Fearing genocide, President Obama authorized the first major U.S. intervention against the Islamic State—air strikes on ISIS positions in Iraq, and air drops of humanitarian goods to the Yazidis. However much Obama wanted to avoid another Middle East war, the United States had just taken on ISIS. The question was what to do next.

The crisis on Mount Sinjar eased after Kurdish fighters—including Mazloum’s militia—created an escape route for the Yazidis into Syria. Pentagon officials took note. The United States had struggled to identify or create a credible rebel force in Syria. Mazloum had a standing militia that proved it could fight, even with only vintage weapons. Between 2011 and 2013, without foreign support, it had pushed Syrian government forces out of northern Kurdish towns during the Arab Spring and fought off an Al Qaeda franchise that moved on Kurdish turf. A senior U.S. military official looked for an introduction. The United States was not the only country interested in the Kurdish general, U.S. officials told me. On the morning of August 18th, Mazloum met Qassem Suleimani, the head of Iran’s Quds Force, the most élite unit in the Revolutionary Guard. The Iranians had rushed in—faster than the Americans did—to help the Iraqis hold off the ISIS juggernaut. Hours after meeting the Iranian commander, Mazloum rendezvoused with the American official in Suleimaniya, a Kurdish city in northern Iraq.

Mazloum came with complications, however. His original militia was the People’s Protection Units, or Y.P.G.; it was Kurdish. Its political arm sought autonomy in Syria. Many of its members, including Mazloum, had trained with a militant Turkish movement—the Kurdistan Workers’ Party, or P.K.K.—which was waging an insurgency to win autonomy in Turkey. The P.K.K. was on the U.S. and Turkish lists of terrorist organizations. Its leader, Abdullah Öcalan, had lived in the Kurdish region of Syria for two decades before he was imprisoned, in 1999, in Turkey. Öcalan was a personal friend of Mazloum’s; they were once photographed swimming together in the Euphrates River. “For a period of time, I served in P.K.K. ranks,” Mazloum told me. “Öcalan was working here, and the people here had loyalty to him. But the Y.P.G. is not a terrorist organization. Always the Turks like to paint everything in Syria like it’s the P.K.K., but this is not true.” Yet Mazloum has relatives who are still with the P.K.K. Huge posters of Öcalan adorned every Y.P.G. and S.D.F. base I visited.