NORTHERN SYRIA — The Islamic State militant group's leader Abu Bakr al-Baghdadi was betrayed by a member of his inner circle who helped lead American troops to his compound in northern Syria.

The commander of the Kurdish-led Syrian Democratic Forces, Gen. Mazloum Abdi, gave NBC News a detailed account of how he spent months running a spy inside ISIS who followed al-Baghdadi as he moved from one safe house to another, until he was finally trapped in a tunnel beneath one of them.

Abdi did not identify the source, but Kurdish intelligence officials described him as an Arab who had many relatives in ISIS.

Abdi said the informant’s main motivation was revenge.

“I think he was under a lot of pressure from his family,” the general said. “His relatives were subjected to harsh treatment by ISIS and he no longer believed in the future of ISIS. He wanted to take revenge on ISIS and al-Baghdadi himself.”

The spy was in a unique and extremely rare position to exact his revenge.

“He was, you could say, a security official,” the general said. “A personal security official for al-Baghdadi himself, in charge of al-Baghdadi’s movements. “

Part of the informant's job, Abdi said, was “securing the places” where al-Baghdadi would later hide.

This ISIS spy memorized the locations and layouts of al-Baghdadi’s safe houses and even stole samples of the world’s most wanted terrorist’s blood and clothing for DNA analysis, he said.

None of that was easy.

“Al-Baghdadi took his security precautions to the highest level,” Abdi said. “He never used high-tech communications at all. Any place he was in, was in a communications blackout, with exception of those who were directly responsible for his security, and that was a small group of people.”

After years on the run, al-Baghdadi had a relatively small household.

“His direct family, the children, his relatives, his siblings, they are formed a tight ring around him,” Abdi said. The ISIS leader only allowed a small group of outsiders to meet him. One of them was the spy.

Fighters from the Syrian Democratic Forces (SDF) prepare to withdraw from the Sanjak Saadoun border area near the northern Syrian town of Amuda, on Oct. 27, 2019. Delil Souleiman / AFP - Getty Images

Abdi declined to say when or how they first made contact with the ISIS informant, but he said the relationship deepened and expanded dramatically in the last five months. That coincided with al-Baghdadi's move from a hideout in eastern Syria to the Idlib province in western Syria, near the Turkish border.

“We confirmed that (al-Baghdadi) had been moved to Idlib in April of this year,” Abdi said.

Idlib was an unlikely place for the ISIS leader to hide. The province is largely controlled by other Islamist groups, including one linked to al Qaeda that is often called the Al-Nusra Front. The group has, at times, fought against ISIS. Abdi said al-Baghdadi was hiding among a pocket of supporters, in what was largely unfriendly territory.

“The idea that al-Baghdadi was in Idlib was completely unexpected,” Abdi said. “It was a surprise to everyone.”

Idlib is a large province with a varied terrain, including hills, canyons, olive groves, and several large towns and cities. Abdi said the spy’s meetings in Idlib were frequent but inconsistent. Kurdish intelligence officials said the spy could not approach the ISIS leader at will, but had to wait to be called for meetings. The face-to-face meetings — ostensibly to talk about security, movements, transportation and setting up future safe houses — would turn out to be critical.

To figure out where exactly al-Baghdadi was holed up in Idlib province, Abdi said the spy had to rely on his senses and memory. Al-Baghdadi’s bodyguards would pick him up in a car, or sometimes a taxi. Most visitors to al-Baghdadi were blindfolded on the journey to the ISIS leader. But the spy was more trusted than most. Al-Baghdadi’s personal guards merely asked him not to look out the car windows.

“When they approached the area, they would ask him to lower his seat so he can't look around,” Abdi said. “They asked him to lie down, to lower the seat in the taxi.”

But even with the seatback flat, Abdi says the spy was able to pick up bits of the topography and could tell if he was in a town or in the countryside, among olive groves or open fields.

Fighters from the Syrian Democratic Forces (SDF) take part in a funeral procession for Kurdish fighters killed during clashes in the northeastern Syrian town of Ras al-Ain, in the town of Qamishli, Syria on Oct. 22, 2019. Muhammad Hamed / Reuters

Once inside al-Baghdadi’s hideouts — and there were several, in close proximity to each other — the spy was able to look around freely, the general said.

He started memorizing the internal spaces and distinguishing structural features that could be seen from above, like a red water tank on a roof.

Those details, Abdi said, were fed constantly back to the Kurds and, through them, to American intelligence agencies, enabling U.S. aerial surveillance to pinpoint al-Baghdadi’s final hideout. The descriptions of the compound helped American commandos plan their assault.

“He provided information about the house itself, the shape of the house and things to do with the house, the specifications of the house,” Abdi said.

Including details about the tunnel where al-Baghdadi blew himself up with the U.S. forces chasing after him.

“We learned that there was a tunnel in the house,” Abdi said. “We learned how many people in the house, how many guards were in the house. We learned the closest al-Nusra checkpoints near the house. We learned all the security details of the house.”

But before sending in the commandos, Abdi said, U.S. intelligence wanted concrete proof that the source had the access to al-Baghdadi that he claimed.

They wanted to make sure it wasn’t all a hoax, or worse, a trap, Abdi said.

So the spy stole a pair of the ISIS leader’s used underwear and later a blood sample to compare with known samples of al-Baghdadi's DNA. Al-Baghdadi had been in U.S. custody in Iraq years before.

Abdi said the spy stole underwear roughly three months ago from a house al-Baghdadi had previously used and abandoned. He wouldn’t say how the blood was collected, only that it was taken about a month ago. Abdi said both DNA tests matched, proving the spy’s bona fides.

“After that, the CIA took this more seriously,” Abdi said. “They began to work hard and serious on the highest level.”

But the timetable for launching a raid on al-Baghdadi’s compound was delayed by President Donald Trump’s sudden decision to pull U.S. troops out of northern Syria and the subsequent Turkish invasion.

Suddenly, Abdi said, his Kurdish-led forces had to focus more of their attention on defending themselves.

Meanwhile, al-Baghdadi was preparing to move yet again.

“Al al-Baghdadi had prepared a new house for himself in a different place located in the area of Dera al-Fraat (Jarablus area),” Abdi said. “That house was ready. I assume that within 48 hours he would have left the house to the new house, and the new house was completely different and wasn't known.”

Abdi said the spy was at the compound when U.S. Special Forces attacked.

“He was there and he returned safely with the American forces,” he said.

U.S. officials have told The Washington Post he’ll likely receive all or part of the $25 million bounty for al-Baghdadi’s head.