On Monday, Iran and the United States, along with envoys from Britain, China, France, Germany, and Russia, will meet again in Vienna to work on specific terms for a nuclear agreement. The talks resume just as Washington and Tehran suddenly find that they have common cause in preventing Iraq’s abrupt disintegration. For both, their longtime strategies toward Iraq appear to be failing, as a few thousand thugs in the Islamic State of Iraq and Syria (ISIS) burn their way across the country.

Washington and Tehran have started using the same language. President Obama, in his remarks on the South Lawn of the White House on Friday, said, “Nobody has an interest in seeing terrorists gain a foothold inside of Iraq, and nobody is going to benefit from seeing Iraq descend into chaos.” An hour later, Iran’s Foreign Minister, Javad Zarif, told me, by telephone, from Tehran, “It is in the interest of everybody to stabilize the government of Iraq. If the U.S. has come to realize that these groups pose a threat to the security of the region, and if the U.S. truly wants to fight terrorism and extremism, then it’s a common global cause.”

Obama said that Washington is “going to pursue diplomacy” across the region. Zarif told me that he’d been working the phones with Iraq’s neighbors for the past two days. Obama warned of the dangers of the Sunni extremists trying to “overrun sacred Shia sites.” Iran is the world’s largest Shiite country, and its interests in Iraq are focussed on protecting the Shiite plurality that was long dominated by a Sunni minority.

Twitter pundits are already speculating about the potential for de facto coöperation between the countries. Among the scenarios: U.S. drones striking ISIS targets and, in effect, providing air cover for Iranian Revolutionary Guards dispatched to help hold back the ISIS jihadis, who have been pushing toward Baghdad. In our conversation, Zarif denied reports that Tehran has already dispatched battalions of Revolutionary Guards to aid and protect Prime Minister Nuri al-Maliki’s government, but its élite Quds Force has long had a presence—in various forms—inside Iraq.

Iran was America’s nemesis throughout the eight-year American war in Iraq. American officials regularly berated the Iranians for providing arms (including I.E.D.s) and strategic guidance to Iraqi militias. “They are responsible for providing the weapons, the training, the funding and, in some cases, the direction for operations that have indeed killed U.S. soldiers,” General David Petraeus told reporters in 2007.

Some forty-four hundred Americans died in Iraq. In 2010, James Jeffrey, the U.S. Ambassador there, estimated that Iran was linked, through its surrogates, to the deaths of more than a thousand American troops. “My own estimate, based just upon a gut feeling, is that up to a quarter of the American casualties and some of the more horrific incidents in which Americans were kidnapped … can be traced without doubt to these Iranian groups,” he said.

Even after Washington announced its intent to leave Iraq, Defense Secretary Robert Gates charged that Iran’s support for Shiite militias was intent on “killing as many as possible in order to demonstrate to the Iraqi people that, in effect, they drove us out of Iraq at the end of the year.”

When the United States ended its combat mission, in 2011, it did not leave even a residual force behind, because Iraq—under Iran’s strong influence—refused to sign a Status of Forces Agreement granting immunity to U.S. troops for acts deemed criminal under Iraqi law. (In the nineteen-sixties, an identical controversy over giving U.S. troops immunity in Iran led to protests by Ayatollah Khomeini and, eventually, his expulsion by the Shah. From exile, in Iraq, Khomeini waged the campaign that ultimately led to the Shah’s overthrow.)

Today, Tehran, like Washington, seems preoccupied with the rise or return of Sunni militants, from Syria to Afghanistan. “Everybody is threatened by this extremism,” Zarif told me in March. “It has changed the strategic calculations and considerations for everybody who is interested in peace and stability in this region…. I think we all must take cognizance of the fact that this is a threat and work on it together—not against one another.”

Above: Iran’s President, Hasan Rowhani, in August, 2013. Photograph by Atta Kenare/AFP/Getty.