One man’s nay could have undone it all. But Iran’s supreme leader, Ayatollah Ali Khamenei, has been working for some time to engineer a way out of the economic and diplomatic quagmire of sanctions. Soon after Mr. Rouhani spoke to reporters, Ayatollah Khamenei issued a short message online saying he considered the deal a success.

“The nuclear negotiating team deserves to be appreciated and thanked for its achievement,” Ayatollah Khamenei said, according to the semiofficial Fars News Agency. He added that “their behavior can be the basis of the next wise measures.”

Ayatollah Khamenei had spoken of negotiating directly with “the great Satan,” the Iranian ideological label for the United States, as long ago as March, three months before Mr. Rouhani was elected president promising better relations with the West. “I am not opposed,” Ayatollah Khamenei said on the subject during his annual address on the first day of the Iranian year, March 21. “But first the Americans must change their hostility towards Iran.”

At the time, few observers thought the remark, made amid a flurry of verbal attacks on the United States, reflected a serious change in policy. But Ayatollah Khamenei apparently allowed a group of Iranian diplomats to begin secret preparatory talks with American officials in Oman, according to an Associated Press report citing American officials. He also assured that the next president of Iran would follow a line different from the prickly hostility of Mahmoud Ahmadinejad, whose comments about Israel and the Holocaust had helped make Iran a pariah.

“It is clear that any international outreach could not be handled by someone like President Ahmadinejad,” said Amir Mohebbian, a political strategist who advises Iranian leaders and is often briefed on Iran’s relations with America. “I think the leader helped bring Mr. Rouhani to power to make the public ready for a policy change.”