LONDON — A bloody clash between the Afghan Taliban and followers of the so-called Islamic State late last month in western Afghanistan has exposed critical shifts in alliances in a war the United States is still trying to fight. Most important, there appear to be consultations, at least, between the Taliban and Iran about how to meet the growing threat posed by the extremists of ISIS, as the self-proclaimed Islamic State is widely known.

An Afghan Taliban commander and former provincial governor in the days when one-eyed Mullah Omar ruled Afghanistan tells The Daily Beast that the battle on May 24 in the Khaki Safe area of Farah province near the Iranian border left at least 22 people dead, including 13 associated with ISIS and nine Taliban. “This was the biggest fight between the Afghan Taliban and ISIS followers so far,” said the former governor, who is still deeply involved with Taliban military operations and asked that his name not be used.

An ex-Taliban who recently switched allegiance to ISIS, Mullah Khan Muhammad Noorzai, tells The Daily Beast that the Taliban attacked the ISIS camp and killed innocent followers of the self-declared ISIS caliph known as Abu Bakr al-Baghdadi.

“Most of those killed were our friends and colleagues,” Noorzai told The Daily Beast in a telephone interview. He said there had been an understanding between the Taliban and ISIS that they would not open fire on each other until they had had a chance to discuss matters, but the Taliban broke that agreement and launched an attack.

“A Muslim won’t break a commitment,” Noorzai declared. “The Taliban are not true Muslims, and not mujahideen [holy warriors] either. Now our jihad against the ignorant so-called Taliban will go on forever.”

Noorzai says the Taliban use the name of Islam, but get money from the Iranians and the United States (which would come as quite a surprise to U.S. officials). According to Noorzai, “The Taliban shadow governor for Farah, through the mediation of Mullah Baz Muhammad, a local elder, swore an oath by Allah and the Quran that they would not attack ISIS, but they did.”

Iran played a key role in this, according to Noorzai. “I was not at the camp at the time of the attack but it was coordinated by the Iranians. There were a few Iranians making films of the dead ISIS fighters.”

The lumping together of supposedly heretical Iran and the Great Satan United States as one common enemy is typical of ISIS followers, whose takfiri version of Islam depends on vilifying and marking for death all who do not follow their own extremely narrow interpretation of the faith. As we have seen in Iraq and Syria, ISIS conceives of Shiite Iran and Shiite Arabs as its greatest foes, marked for conversion or extermination. When other Sunni groups resist its interpretations, they too are marked as non-Muslim heretics, even, it would seem, if they are the infamously fundamentalist Taliban.

“Iran is our No. 1 enemy,” says the 38-year-old Mullah Noorzai. “If Sheikh Abu Bakr sends the order, we will definitely cross from Afghanistan to Iran.” He said his group leaders are Mullah Abdul Malik and Mullah Mansoor, both of whom were lucky to survive the Taliban attack. “Now,” said Noorzai, “we will take revenge against the Taliban.”

“I fought for Mullah Omar for 20 years and I regret that very much,” said Noorzai. “They just serve the interest of Pakistan, not Islam.” Sunni Muslims dominate the Pakistani intelligence services, which helped create and nurture the Taliban, and many of its intelligence officers consider themselves zealous believers. But they, too, are deemed deficient and heretical by ISIS.

“I lost a few of my fingers in the fight for Kabul in 1996 that brought the Taliban to power in Afghanistan, but they are clueless and useless as Muslims,” said Noorzai.

Another ex-Taliban splinter group known as Fidai Mahaz, after its leader Mullah Najib Fidai Mahaz, also criticizes what it deems close relations between the Taliban and Iran. Its spokesman, Qari Hamza, told The Daily Beast in another phone interview that the Taliban-Iran relationship is wasting the blood and sacrifices made by Taliban fighters in the past.

A few days before the battle of Khaki Safe, a high-ranking Taliban delegation visited Iran to talk with senior officials there about “regional issues.” It was led by Muhammad Tayyab Agha, the head of the group’s political office based in Qatar, and a Taliban spokesman said at the time it was an “ordinary” mission.

But Hamza claims the Agha visit came at the urgent request of Tehran. “In fact the Iranians and Taliban are both worried about the rapidly spreading movement of ISIS in Afghanistan,” he said. He also said: “Iranian intelligence gave $3 million cash and it was received on the Iran-Afghan border. Iranians also pledged to supply 3,000 arms and 40 Ranger pickup trucks and ammunition to the Taliban to fight against ISIS near the Iran-Afghan border.”

Another ex-Taliban cabinet minister, who insisted on anonymity, told The Daily Beast that Tayyab Agha normally does not attend any peace talks or political meetings. ‘Tayyab only goes to meetings related to the military and to secret deals.”

Several Taliban sources say that high-level contacts between Iran and the Taliban based on mutual interests, including hostility to the United States, have been going on for years. “For a long time the Iranians were secretly saying to the Taliban, ‘If need be we would not mind Mullah Omar crossing onto Iranian soil,’” according to the former Taliban minister. He said Tayyab Agha, for many years one of Mullah Omar’s most trusted aides, is still a key figure among the Afghan Taliban and his visit to Iran was meant in part as a message to Pakistan.

For the last eight months, Pakistan has put increasing pressure on the Afghan Taliban to go for peace talks with the government of President Mohammad Ashraf Ghani in Kabul. “Tayyab Agha’s visit to Iran could be a punch in the face of Pakistan. The message is clear: If the Taliban get into trouble in Pakistan, the Taliban have other options [for support].”

Students of the region’s history will remember that there were once bitter and bloody differences between the Taliban and Iran. In 1998, when the Taliban took the city of Mazar-i-Sharif they slaughtered several Iranian diplomats in an incident that almost led Tehran to declare war. The Iranians also provided support over the years to non-Pashtun fighters opposed to the Taliban, including the late Ahmad Shah Massoud.

But that is old history and changes in the situation on the ground, including the rise of ISIS, dictate changes in policy. The former Taliban minister says that generally the Iranians did not support the “War on Terror” in the aftermath of 9/11 and that made it easier to find common ground. Now, he says, “Across the border in Afghanistan the existence of ISIS is a challenge for the Taliban and a headache for Iran. That is the point making Iran and the Taliban closer to each other.”

Muhammad Naeem , a spokesman for the Afghan Taliban in Qatar, rejects reports that the Taliban are getting money from the Iranians. “Yes, the Taliban visited Iran for a bilateral meeting and discussions, but reports about receiving funds are not correct,“ Naeem told The Daily Beast.

U.S. President Barack Obama has said that the United States, which still has about 10,000 troops in Afghanistan, will end its involvement there by the end of next year.