Mr. Obama began reaching out shortly after taking office in 2009, writing the first of what would be four letters to Ayatollah Khamenei. It was not until last year’s election of Hassan Rouhani, followed by his choice of Mr. Zarif, that doors really began to open and Mr. Obama authorized a secret channel to the two men through Oman.

His envoys, William Burns and Jake Sullivan, both then top administration officials, traveled with little or no entourage, slipping into the back doors of hotels. Israel was kept in the dark for months, as were the French.

The talks moved to New York in September 2013 under the cover of the United Nations’ annual meeting. Mr. Zarif met Mr. Kerry in a closet-size room near the Security Council chamber, and the two exchanged private telephone numbers and email addresses, a channel they have used more than either has publicly admitted. Mr. Zarif helped engineer a telephone call between Mr. Obama and Mr. Rouhani, the first direct contact between American and Iranian leaders since the 1979 revolution. “It cost us when we got home,” Mr. Zarif later noted.

But the talks led to a deal last November to freeze much of Iran’s nuclear activity in exchange for some sanctions being lifted while formal talks for a broader agreement were held. Wendy Sherman, the under secretary of state, led the new negotiations so persistently that she kept going even after rupturing a finger in a fall and later breaking her nose on a glass door in Vienna.

Iran threw several curveballs. Ayatollah Khamenei said in a speech that Iran would ultimately increase its capacity to produce enriched uranium tenfold, rather than decrease it. “Zarif all but told us he didn’t see that coming,” an American official said.

Mr. Zarif then surprised Mr. Kerry in July by proposing in an interview with The New York Times that Iran would simply continue the temporary freeze for seven years or so but dismantle nothing. “He’s negotiating in public,” Mr. Kerry fumed. Another American official said “it didn’t even accord with what he was saying to us” privately. But it helped give Mr. Zarif room with hard-liners at home to extend the first deadline.

Negotiators reconvened in late September in New York, but the Iranians told the Americans they would not consider real offers until after the midterm elections. The Americans said that was silly; the talks were not an issue in the elections. Mr. Kerry became more heavily involved. He began meeting with Mr. Zarif, either alone or, to keep the other partners in the loop, in three-way meetings with Catherine Ashton, the European Union envoy to the negotiations.