As pressure built up for Nouri al Maliki to step down from the prime minister post in Iraq last month, Iran, his most ardent supporter stayed surprisingly silent as top Iranian officials worked to get him out.
Similarly, when the United States, regularly denounced as the Great Satan by officials in Iran, began bombing inside Iraq last month, Tehran stayed quiet.
This marked shift in Iran's approach to Iraq is a response to the gains of Islamic State, the militant group which has torn across Iraqi territory and come within striking distance of the Iranian border.
Islamic State fighters in Iraq have engaged in acts of brutality, including beheadings and mass executions, often targeting Shi'ites, whom they consider to be heretics. The majority of Iranians are Shiite Muslims.
As a result of this threat, Iran has had to take a more flexible approach to its policy in Iraq, which has led to a series of dramatic shifts, experts say.
Not only have officials in Tehran dropped their support for Maliki and turned a blind eye to renewed U.S. attacks in Iraq, they have also reached out to arch rival Saudi Arabia and participated in talks about the security situation in Iraq.
"There's a drastic change in Iranian foreign policy with regard to Iraq," said Mehdi Noorbaksh, an associate professor of international affairs at Harrisburg University of Science and Technology.
The last time that Iraqis chose a prime minister, in 2010, the bloc led by Ayad Allawi won the most seats in the election.
Allawi, a moderate Shi'ite, had pulled together a coalition that included Shi'ites and a number of prominent Sunni politicians but Maliki, not Allawi, became prime minister, largely due to pressure from Iran, critics say.
Allawi, who has held talks with the current prime minister designate Haidar al Abadi for a potential role in the new government, is still critical of the Iranian influence at that time. "It's deprivation of the Iraqis from their rights by a foreign power," he said. "It's insulting to the Iraqis."
When parliamentary elections were held in Iraq last spring, the situation had hardly changed: Iran continued with its steadfast support for Maliki. It was only when the Islamic State captured Mosul in mid-June that Iranian officials grasped the direct threat posed to Baghdad and the holy Shi'ite shrines in the country and changed tack, Iraqi officials and experts say.
"Iran was supportive of Maliki and said to hell with the others until the army collapsed," said a senior Iraqi official who asked not to be identified because of the sensitive subject.
"Iranians are very realistic people, very patient. They weigh their national interests very carefully. They don't want a front with the Islamic State that extends from Iran all the way to the Mediterranean," he added.
The importance of the issue for the Iranians was highlighted when the commander of the Revolutionary Guards Quds Force, Qassem Soleimani, travelled to Baghdad in June. The Quds Force is a branch of the Revolutionary Guards that is tasked with operations outside of Iran, frequently involving proxy armed groups in the region.
Soleimani met top Iraqi security officials to help organise a military counter-offensive to the advance of the Islamic State, current and former Iraqi officials say. The plan included the use of thousands of militiamen who were armed and trained by Iran as well as thousands of new recruits who had volunteered after Iraq's most senior Shi'ite cleric, Ayatollah Ali Sistani, issued a call to arms against the Islamic State in June.
But Soleimani also met Maliki to discuss the prime minister post. The Iranians were disappointed by Maliki's inability to rally the military to confront Islamic State. "They curse Maliki," said a former senior official who asked not to be identified because of the sensitivity of the subject matter.
Soleimani's outreach was followed by a visit from the director of Iran's Supreme National Security Council, Ali Shamkhani, who met Sistani along with a number of other prominent Shi'ite clerics and Sunni politicians in mid-July.
Shamkhani's visit was significant because he is not only a top security official but also a relative moderate who is close to president Hassan Rouhani and the Supreme Leader, experts say. One week after meeting with Shamkhani, Sistani, who had been implicitly pushing for Maliki to step down, issued a statement saying political leaders should not cling to power.
When Iraq's president announced that Abadi was the new prime minister candidate in mid-August, Shamkhani sent a message of congratulations even before Maliki had announced whether he would step down.
"Iran was one of the first countries that supported Abadi," Noorbaksh said. "Through Shamkhani they wanted to say the whole security apparatus of Iran is behind the new prime minister."
Saudi Arabia, a largely Sunni Muslim country long critical of what it views as Maliki's sectarian Shi'ite policies, also praised the nomination of Abadi. Despite deep divisions between Saudi Arabia and Iran over the war in Syria in recent years, the two countries now face a mutual threat from Islamic State. Fighters from the militant group have threatened to attack Saudi Arabia in videos posted on the Internet.
Iran broke through the diplomatic impasse last week and sent deputy foreign minister Hossein Amir Abdollahian for an official visit to Jeddah, the first meeting of its kind since Rouhani became president one year ago. Abdollahian discussed the growth of Islamic State in Iraq among other topics with his Saudi counterpart.
But even more surprising than the thaw in relations between Iran and Saudi Arabia is Iran's tacit acceptance of American bombing in Iraq nearly three years after the last U.S. troops left the country. Here, again, Iran reversed its traditional foreign policy stance in the face of a mutual threat.
"The Islamic State is a common enemy to both the United States and Iran," said Iraq's Foreign Minister Hoshyar Zebari. "I've always said that even at the most difficult times they have a common interest, even if they don't confer."
Iranian hardliners have not criticized the American attacks in Iraq and even conservative press outlets, which often lambast the U.S., have kept largely quiet.
"There was no criticism whatsoever," said Noorbaksh.
"This is a huge sign that the Iranians do not mind. You cannot find anything in Khamenei's speeches criticizing the United States inside Iraq now."
In contrast, Khamenei has criticized the U.S. on a broad range of other issues in recent speeches, including the American position in talks between Iran and Western powers on Iran's nuclear program.
Additional reporting by Ned Parker in Baghdad; editing by Anna Willard