Maysam Behravesh is a political analyst at Persis Media and PhD candidate at the department of political science at Sweden's Lund University. He was an intelligence analyst and policy advisor in Iran from 2008 to 2010.

Unprecedented negotiations in Qatar between US special envoy to Afghanistan Zalmay Khalilzad and the Taliban have created cautious optimism for lasting reconciliation. “We made significant progress on vital issues,” Khalilzad wrote on Twitter, adding: “Nothing is agreed until everything is agreed, and ‘everything’ must include an intra-Afghan dialogue and comprehensive ceasefire.”

The potential peace pact, according to a Reuters report, stipulates that foreign forces leave Afghanistan within 18 months of the draft deal being signed. It also requires the Taliban to prevent al-Qaeda and the Islamic State of Iraq and the Levant (ISIL, also known as ISIS) from using Afghan territory to stage attacks against the United States and its allies.

The big elephant in the room, however, is how the consequent power void will be filled and what role, in particular, Afghanistan’s neighbours, Iran and Pakistan, will play in the post-withdrawal environment. While Pakistan has generally backed the Afghan Taliban as an ideologically reliable and militarily resilient bulwark against growing Indian influence in its northern neighbourhood, Shia Iran’s relationship with the Sunni militant group is highly complicated. Part of this complication is rooted in their common opposition to the US, which has led Tehran to pursue a costly and unpopular policy of “strategic hedging” in war-torn Afghanistan, that is, supporting the US-backed Afghan government and the Taliban at the same time and playing them against each other whenever necessary.

The Tehran-Taliban rivalry

Apart from their sectarian and ideological differences, the Islamic Republic and the “Islamic Emirate” – as the Taliban prefer to describe themselves – have a bitter history of geopolitical rivalry.

In September 1996, the Taliban fighters seized control of Kabul and established the so-called Islamic Emirate of Afghanistan, which was recognised by Pakistan, Saudi Arabia and the United Arab Emirates – all Iran’s regional rivals. Around two years later, in August 1998 when the Taliban were fighting Iran-backed Northern Alliance – a military front headed by former Afghan Defence Minister Ahmad Shah Massoud – to extend their control over all Afghan territory, they stormed the Iranian consulate in the northern city of Mazar-e-Sharif and killed 10 diplomats as well as a correspondent from Iran’s state news agency there.

Shocked and outraged by the mass murder of its diplomats, Tehran deployed tens of thousands of troops along the border with Afghanistan, but ultimately stopped short of invading its eastern neighbour. It was later speculated that members of Sipah-e-Sahaba, an anti-Shia militant organisation with close connections to the Pakistani military and intelligence apparatus, played a role in the killings.

Pakistan’s newfound nuclear capability must have been on the minds of Iranian leaders when they decided to refrain from a military intervention against the Islamabad-backed Taliban forces in Afghanistan. After three years of strategic self-restraint and patience, however, Iran grasped the opportunity presented by the September 11 terror attacks and, along with the Northern Alliance, provided the US-led coalition with indispensable military and intelligence assistance to topple the Taliban government in late 2001.

Ever since, Tehran’s evolving relationship with the Taliban has been marked by an ambivalent combination of restrained support and containment, as Iranian leaders have sought to use the armed group as a strategic instrument to stymie the US war effort in Afghanistan and make sure that the American military presence in Iran’s backyard remains contested and abortive.

In September 2017, Sharif Yaftali, chief of general staff of the Afghan national army, affirmed in an exclusive interview with BBC that Kabul had evidence showing that Iran supplied weapons and equipment to the Taliban in western Afghanistan. The revelation followed similar remarks by provincial authorities in the region. In August of the same year, Mohammed Arif Shah Jehan, then governor of Farah province adjacent to Iran, told The New York Times that “the strongest Taliban” in the border areas were “Iranian Taliban”.

Another sign of a possible Tehran-Taliban collaboration emerged in May 2016, when a US drone targeted a lone vehicle on a desert route in the southwestern Pakistani province of Balochistan, killing the driver and his only passenger. While the passenger’s passport listed his name as Wali Mohammad, a Pakistani national, US and Afghan officials confirmed his real identity as Mullah Akhtar Mansour, then-leader of the Taliban. Intriguingly, the obscure passenger was returning from Iran, where his passport indicated he had travelled at least twice during the year.

A key catalyst for the Islamic Republic’s non-aligned yet convergent relationship with the Taliban has been the rise of the more militant, vehemently anti-Shia ISIL in parts of Afghanistan. For Iran, the Taliban is, among other things, the lesser nemesis with the potential to curb the spread of the most aggressive anti-Iranian forces to its east.

As Tehran treats the Taliban as an ancillary lever in its strategic toolkit, the Sunni militant group views neighbouring Iran through a similar lens of expediency.

It is “a seasonal relationship that serves only one purpose and that is to fight the Americans out of Afghanistan. It is, thus, a hate-love relationship”, Harun Najafizada, veteran Afghan journalist and translator into Persian of Zalmay Khalilzad’s book, The Envoy, told me. “Aware of how the Islamic Republic treats fellow Sunnis in Iran, the Taliban does not seem to recognise Shia Iran as a strategic ally”.

Taliban in the post-war government?

With the US-Taliban peace negotiations gaining momentum and the Trump administration searching for a safe way to withdraw American troops from Afghanistan, Tehran’s contacts with the armed group have recently increased. Officially confirming these communications, the Secretary of Iran’s Supreme National Security Council Ali Shamkhani stressed the need for “the active participation of regional countries” in the Afghan stabilisation and peace process during a one-day visit to Kabul on December 26 last year. Shamkhani’s assertions, however, did not stir as much controversy as those of Iranian Foreign Minister Mohammad Javad Zarif who described a future Afghan state without the Taliban as “impossible” in a January interview with India’s NDTV channel.

“I think it would be impossible to have a future Afghanistan without any role for the Taliban. But we also believe that the Taliban do not have, should not have, a dominant role,” Zarif said.

In a harsh reaction, an official at the Afghan presidential office criticised Zarif for acting as a “Taliban spokesman”. “If the neighbouring country believes in dialogue for finding solutions, why doesn’t it use the same method to resolve its own domestic political problems? In fact, Iran’s government fears the current freedoms in Afghanistan may turn into a model for Iranians,” President Ashraf Ghani’s deputy spokesman Shah Hussein Mortazavi wrote on his Facebook account before deleting the post.

Nonetheless, some Afghans believe that Kabul generally pursues a “very cautious” policy towards Tehran. “The government is usually silent about Iran’s actions because they need its financial and political backing,” Faisal Karimi, a professor of journalism at Herat University and director of the Afghanistan Institute for Research and Media Studies, said. “They are trying to create a balance in Afghanistan’s foreign policy and think keeping Iran satisfied would serve Afghan interests”.

It is the quest for a similar strategic equilibrium that inclines the Islamic Republic to favour a heterogeneous, if not divided, government in post-withdrawal Afghanistan. Iranian leaders seem to believe that such a pragmatic balance would maximise their control over Iran’s eastern neighbour, preventing it from turning into an anti-Shia stronghold on the one hand and a US political-military bastion on the other.

The views expressed in this article are the author’s own and do not necessarily reflect Al Jazeera’s editorial stance.