Two days before he was shot dead by the Taliban, Abu Omar Khorasani, a onetime leader of Islamic State in Afghanistan, sat slumped in a dingy Afghan prison interview room, waiting for his soon-to-be executioners.

Mr. Khorasani saw the Taliban’s advance as a harbinger for change. For years both organizations had sworn to rid Afghanistan of nonbelievers.

“They will let me free if they are good Muslims,” he told The Wall Street Journal in an interview.

When Taliban fighters seized Kabul last week, they took control of the prison, freed hundreds of inmates, and killed Mr. Khorasani and eight other members of his terror group.

Just as the Taliban has been fighting American coalition forces in Afghanistan, it has been waging a separate but parallel war against its rival Islamist group.

On one side are the Taliban, who have co-opted remnants of al Qaeda. On the other is the Afghan arm of Islamic State, known as ISIS-K, which has sought to incorporate parts of Afghanistan into a broader caliphate emanating from the Middle East.

The Taliban, assisted at times by other countries and U.S. coalition forces, were the winner in that effort, defense officials say. ISIS-K has been driven from its enclaves in Afghanistan and its fighters dispersed into hiding. There appeared to be little resistance as the Taliban swept across the country this month in the wake of the U.S. withdrawal.

Two powerful explosions, attributed by the U.S. to an affiliate of Islamic State, caused havoc on Thursday.

Photo: Wakil Kohsar/Agence France-Presse/Getty Images

On Thursday, in a reminder that the battle remains bloody, two explosions ripped through the crowds surrounding Kabul airport, where the Taliban and U.S. forces had been providing security to foreigners and locals seeking to flee.

At least 90 Afghans and 13 U.S. service members were killed, the Pentagon said. U.S. officials attributed the attacks to Islamic State’s regional affiliate. Islamic State claimed responsibility in a report posted by its Amaq news agency.

The continued presence of Islamic State in Afghanistan is one reason the Taliban could receive international support from countries, including the U.S., that view Islamic State as a profound threat.

Russia, China and Iran say they see Taliban as a mainstay of stability in Afghanistan—a reason they plan to keep their Kabul embassies open after the U.S. withdrawal.

During a news conference after Thursday’s attack, Marine Corps Gen. Frank McKenzie, commander of U.S. Central Command, said the U.S. was relying on the Taliban to screen Afghans as they approached the airport.

“We use the Taliban as a tool to protect us as much as possible,” he said.

When the U.S. invaded Afghanistan in the wake of the Sept. 11 attacks, the Taliban had few allies. The organization was reviled in the West for hosting al Qaeda terrorists, and opposed by regional powers including Russia and Iran.

Behind the appearance of solidarity between the Taliban and al Qaeda was an uneasy relationship, with many Taliban resenting Osama bin Laden for using the country as an operating base starting the late 1990s.

A computer recovered by the Journal in Kabul after the Taliban were ousted in 2001 showed that al Qaeda members often looked down on their Afghans allies as illiterate and incapable of understanding the Quran. Members of the Taliban in turn blamed some in al Qaeda for exacerbating problems with the West and contributing to their country’s isolation.

The Sept. 11 attacks created new fissures as leaders of both organizations were forced into hiding. Taliban founder Mullah Omar didn’t appear to know about the attacks in advance, and his relationship with bin Laden was chilly while both were in hiding in Pakistan, said Anne Stenersen, an academic researcher of Islamism and author of the book “Al-Qaida in Afghanistan.”

After U.S. forces killed bin Laden in 2011, documents recovered from his Pakistan hideout suggest scant contact between the al Qaeda leader and Omar, she said.

The Taliban and al Qaeda forged stronger bonds on the battlefield as both fought U.S. occupation forces.

While the Taliban took years to regroup after 2011, al Qaeda launched the first successful attacks on U.S. troops in the eastern province of Ghazni in 2004, using improvised explosive devices, said one former Taliban commander who fought U.S. and government troops there.

By 2009, the groups began to merge their commands, usually with al Qaeda members embedded alongside Taliban fighter groups, the former commander said. The combined forces of the two groups waged a terror campaign against the U.S.-backed government and coalition forces through hit-and-run attacks, bombings and targeted assassinations.

The dynamic shifted as al Qaeda sought a lower profile and Islamic State rose in prominence in 2015. The new group seized territory in Syria and Iraq, and invited fighters to join to create a province of “Khorasan,” a historical region encompassing parts of Afghanistan, Iran and former Soviet states of Central Asia.

The group found devotees among disaffected Taliban and militants from Central and South Asia, some of whom volunteered for service in Syria and Iraq. Two Islamic State enclaves appeared inside Afghanistan itself; one in the eastern province of Nangarhar and another in the northern province of Jowzjan.

The arrivals weren’t welcomed by the Taliban, which viewed Islamic State as an impediment. Islamic State had more ambitious global goals, while the Taliban sought to regain control of Afghanistan and had no interest in helping Islamist groups outside the country, said Mr. Khorasani in interviews conducted shortly before his death.

Afghan security personnel gather in front of a prison in Jalalabad in early August after an Islamic State group attack on the facility.

Photo: Rahmat Gul/Associated Press

“The leadership of Daesh is independent, the goals of Daesh are independent,” Mr. Khorasani said, using an alternative name for Islamic State. “We have a global agenda and so when people ask who can really represent Islam and the whole Islamic community, of course we’re more attractive.”

Other nations began to view the Taliban as a potential bulwark against Islamic State’s global ambitions.

“There was huge concern about it and suddenly there was a desire to find some common ground with the Taliban,” said Bruce Hoffman, director of security studies at Georgetown University. “People began saying maybe they were a group we could reason with.”

Russia, which still officially classifies the Taliban as a terrorist organization, opened negotiations with the group more than five years ago, according to Ivan Safranchuk, a Central Asia expert and professor at Moscow State University. The rise of Islamic State in Afghanistan “became a motive to go big with these contacts,” he said.

The U.S. has accused Russia of providing arms to the Taliban, an allegation that Russia denies. Iran also has provided arms, according to U.S. intelligence. China separately hosted a high-level Taliban delegation as recently as this year.

Mr. Khorasani said he joined ISIS-K when it opened a chapter in Afghanistan. He rose to be regional governor—its then-highest ranking member—overseeing South Asia and the Far East.

Similar to Islamic State in Iraq and Syria, the group in Afghanistan became infamous for grisly execution videos, attacks on civilian targets, and use of extreme violence against newly conquered locals who opposed their rule.

A village in Nangarhar Province, which has seen fighting by the U.S., Afghan government forces, ISIS and the Taliban.

Photo: Andrew Quilty for The Wall Street Journal

In Nangarhar, where Mr. Khorasani served as governor, the group executed village elders and locals by seating them blindfolded on a pile of explosives on a hillside, which it detonated. The group later circulated a video recording of the execution.

Mr. Khorasani said those executed in the video were criminals.

He said attacks by Islamic State often benefited the Taliban, despite the enmity between the groups. He noted that a prison break in Jalalabad last year, organized by Islamic State and involving four suicide bombers and 11 gunmen, set free hundreds of prisoners from both the Taliban and Islamic State.

A showdown between the Taliban and Islamic State took place in Jowzjan in 2017, Mr. Khorasani said, after a commander with Taliban ties and his fighters swore allegiance to Islamic State founder Abu Bakr al-Baghdadi. They were joined by a militant Uzbek group called the Islamic Movement of Uzbekistan. Together they seized two valleys of the province and raised Islamic State flag over their statelet, Mr. Khorasani said.

The fighting that he described corresponds with U.S. accounts of the battles, in which the forces of the U.S., the Afghan government and the Taliban crushed Islamic State militants over the course of several months. Hundreds of Islamic State militants surrendered to government forces the following year.

In Nangarhar, Islamic State was similarly ground down by attacks by the U.S., Afghan government and the Taliban, Mr. Khorasani said. The U.S. dropped what is known as the Mother of All Bombs, or a MOAB, the most powerful conventional bomb in the U.S. military arsenal, to wipe out a Soviet-era cave complex controlled by Islamic State militants.

The prison at the National Directorate of Security in Kabul held Mr. Khorasani and Taliban prisoners. Mr. Khorasani was executed when the Taliban took over Kabul.

Photo: Victor J. Blue for The Wall Street Journal

“Everyone supported the Taliban one way or another against us,” Mr. Khorasani said. “It’s no secret why they began to win.”

The U.S. said at the time that it killed more than 90 militants including several commanders in the bombing of the cave complex. Mr. Khorasani disputed that, saying the complex was evacuated at the time.

The rise of Islamic State as a new international enemy furthered the Taliban’s global diplomatic efforts, boosting a group that for years had sought to scrub itself of a terrorist taint, according to former officials of the U.S.-backed Afghan government.

The U.S. offered the Taliban international recognition by opening negotiations in Doha that led to the release last year of 5,000 inmates from Afghan prisons. Many of those former detainees flocked to the battlefield, strengthening Taliban forces, former Afghan government officials said.

As part of the agreement reached in Doha, the Taliban promised to prevent militant groups from attacking the West.

Mr. Khorasani said he left Nangarhar last year as the remnants of Islamic State fighters dispersed inside Afghanistan. He was arrested by U.S. and Afghan forces in a house outside Kabul in May 2020.

A judge sentenced him to death and 800 years in prison, he said. The Taliban got to him first.

Write to Alan Cullison at alan.cullison@wsj.com