The operation to kill the Islamic State group’s leader took months of intelligence work, required the snatching of his underwear and ended when one of the world’s most sought-after men blew himself up.

The hunt for Abu Bakr al-Baghdadi — whose extremists have unleashed ghastly violence on civilians across the Middle East and beyond — has been a top Western priority but was complicated by the messy divisions in war-ravaged Syria.

U.S. Special Operation Forces who led the weekend raid relied on intelligence from Kurdish fighters, who just weeks earlier were abandoned by President Donald Trump and forced to leave their positions as Turkey tries to crush them over their links to separatists at home.

Polat Can, a senior adviser to the Kurdish-led Syrian Democratic Forces, said the fighters teamed up with the CIA on May 15 after Baghdadi was spotted in Idlib province.

“Our own source, who had been able to reach al-Baghdadi, brought al-Baghdadi’s underwear to conduct a DNA test and make sure (100 percent) that the person in question was al-Baghdadi himself,” Can wrote on Twitter.

SDF commander General Mazloum Abdi, in an interview with U.S. network NBC, said the fighters cultivated a security advisor deep within Baghdadi’s inner circle who provided them a layout of his home — including the number of guards, floor plans and tunnels.

The informant stole Baghdadi’s underwear three months ago and, later, obtained a blood sample, NBC News said.

The United States had DNA samples on file of Baghdadi, an Iraqi Sunni detained by U.S. forces in 2004 in the flash point city of Fallujah before rising to the helm of the Islamic State group, also known as ISIS.

Surprise location

Like Osama bin Laden, who was killed in 2011 in the Pakistani garrison town of Abbottabad, Baghdadi defied conventional wisdom on where he would be hiding.

The SDF said that Baghdadi had moved from Deir ez-Zor, the desert region where IS made its last stand against a Kurdish-led assault, to a village called Barisha in Idlib, still a major battlefield.

The northwestern province is largely controlled by Hayat Tahrir al-Sham — linked to al-Qaida, which has a rivalry with IS.

Turkey has troops on the ground in Idlib, which Syrian President Bashar Assad’s regime, backed by Russia, is pounding by air.

Can, the SDF advisor, said Turkey’s incursion this month — made possible by Trump’s controversial decision to withdraw some 1,000 U.S. troops — caused a delay in the operation against Baghdadi.

A U.S. State Department official, speaking on condition of anonymity, said only that the United States, mindful of the “chaotic situation,” decided “it was important that we do it now.”

Trump, touting his role in the operation, said U.S. forces informed Russia that they were entering, telling them: “We think you’re going to be very happy.”

Russia, without confirming Baghdadi’s death or Trump’s colorful account, said it did witness U.S. planes and drones in the area of the operation.

One country that was not informed was Syria, with the State Department official saying the United States saw no need to contact Assad’s regime.

Cornered in tunnel

General Mark Milley, the Chairman of the Joint Chiefs of Staff, said the United States coordinated with other militaries through established channels to avoid incidents.

U.S. forces entered the compound by helicopter, quickly neutralized Baghdadi’s forces in a gun fight and then secured the building to protect civilians.

Baghdadi ran into a tunnel and, aware that he was cornered, detonated a suicide vest, killing himself and three children with him, Milley said.

U.S. forces fired missiles, bombs and guns to blow up the compound as they left and disposed of Baghdadi’s mangled body at sea, officials said.

Trump said Baghdadi — whose group has slaughtered and enslaved thousands of civilians — was “whimpering, screaming and crying” as he faced certain death.

Milley did not confirm the detail but said Trump may have heard the account directly from unit members, none of whom were hurt.

Trump described Baghdadi as a “coward” and a “loser” and said he “died like a dog.”

As far as real dogs, one member of the U.S. K-9 unit that chased Baghdadi was wounded but not killed. Milley hailed the dog for its “tremendous service.”