By day nine of the final negotiations over Iran’s nuclear program, diplomats were tired and short-tempered. At a small meeting with the U.S. delegation, the Iranian foreign minister had just begun a familiar speech on how international sanctions were an insult to his country’s dignity and pride, when his American interlocutor cut him off.

“You’re not the only nation with pride,” Secretary of State John F. Kerry snapped.

That morning, July 5, was one of the low points in what became a 17-day marathon of talks leading to this week’s landmark deal with Iran, the culmination of nearly two years of up-and-down negotiations in hotels and conference rooms across Europe and beyond.

There were times when participants were encouraged, and times when the task seemed impossible and there were threats to walk away. When the Vienna endgame started in late June, no one was sure they could reach the finish line.

This account of those weeks is based on public statements and background briefings by participants of several delegations in Vienna and on descriptions provided by U.S. officials who spoke on the condition of anonymity to discuss events that took place behind closed doors.

The Iran nuclear deal marks another milestone in Barack Obama's presidential tenure. Washington Post opinion columnist Jonathan Capehart explains how deeply this affects his legacy. (Jhaan Elker/The Washington Post)

It was not until late Monday that they realized they had accomplished what they set out to do. Early Tuesday, when they knew they had a deal, Iranian Foreign Minister Mohammad Javad Zarif sat one last time with Kerry and U.S. negotiating partners from Britain, France, Germany, Russia, China and the European Union. They asked most of their staff members to leave the room.

Kerry and Zarif, in particular, had barely left the Vienna hotel where their conversations were held for more than two weeks while the world outside debated their efforts. Each was about to head home to the new and possibly more difficult tasks of selling the deal to their countrymen, and making it work.

One by one, in alphabetical order by country, the diplomats spoke about the meaning of what they had achieved and the challenges that lay ahead. Kerry, speaking last, talked about the opportunity they now had and the need for the agreement to be effectively implemented to be credible.

At the end, he spoke about going to Vietnam at the age of 22 and coming home with the belief that no young person should ever have to go through that experience unless nations had exhausted all other alternatives to war. He choked up, and paused, as several others were visibly moved, said two officials who witnessed it.

The diplomats began to applaud.

‘We have nothing to hide’

Talks among technical experts and deputies had been underway almost continuously for months, in an effort to fill in the many blanks in a broad framework for a possible deal the ministers had agreed to in early April. But the most difficult final questions could be answered only by the top diplomats from the negotiating countries.

Which “red lines” drawn during negotiations ended up in the deal

By late June, they were facing several deadlines. An interim agreement made in late 2013, which froze Iran’s nuclear program in exchange for the release of a small portion of Iranian money seized under international sanctions, would expire at the end of the month. In Washington, Congress had demanded that any agreement be turned over to it by July 9.

Kerry flew into Vienna late on Friday, June 26. He was still hobbling on crutches from a May 31 bicycle accident outside Geneva, where he had held a one-day status meeting with Zarif, and had to use a mechanical lift to be raised up to the door of his official aircraft.

They kicked off their talks shortly after noon the next day.

The fundamental principles on nuclear issues already had been decided — Iran would give up most of its enriched uranium and limit future production, effectively eliminating the possibility it could rapidly develop a nuclear weapon, and would agree to intrusive international inspections.

It was the other side of the deal — the end of international sanctions against Iran — that dominated the talks.

On Sunday night, June 28, Zarif flew to Tehran for a day of consultation with Iran’s leadership. Though he was smiling and jocular in public, the Americans considered him a tough negotiator, well-briefed on the often-arcane details of nuclear fuel cycles and U.N. resolutions. But as hard-line public statements came out of Tehran, they were sometimes uncertain whether he had the authority to seal a deal.

The next day, the interim deadline was extended a week.

By the end of the first week, as foreign ministers came and went from U.S. negotiating partner countries — known collectively as the P5+1 — they had agreed that economic and banking sanctions would not be lifted until the International Atomic Energy Agency had verified that Iran’s declared nuclear sites were being used only for the civilian purposes of producing energy and medical isotopes. Verification, called “Implementation Day,” might not happen for months after a deal was completed.

They also agreed on procedures for “managed access” by the IAEA to Iran’s undeclared nuclear sites — places, including military installations, where there was suspected nuclear activity.

“We have nothing to hide,” a senior Iranian negotiator told Western reporters.

On July 4, the Coburg Palace hotel, where the talks were being held while hundreds of international journalists kept vigil outside, held an Independence Day party for the American delegation and U.S. journalists.

Kerry, in shirtsleeves and a tie, hobbled in and ate a cheeseburger. He gave a small speech that seemed to rouse U.S. technical experts and staff, some of whom had not been home for months.

The next day, the talks started to bog down. While the United States and its partners insisted on safeguard procedures to snap back lifted sanctions if Iran violated its commitments on the nuclear side, Iran demanded a procedure to lodge complaints if the P5+1 faltered in its obligations.

“What’s important is equality,” an Iranian diplomat said.

Ready to go home

On the evening of July 5, after their morning exchange on national pride, Kerry and Zarif met in a small room beside a dining facility the Coburg had set up for P5+1 delegations (the Iranians ate in a separate dining room). They were accompanied only by U.S. Energy Secretary Ernest Moniz, a nuclear physicist, and his Iranian counterpart, Ali Akbar Salehi.

Zarif, a U.S. official recounted, wanted to renegotiate the duration of various restrictions on Iran in the emerging deal, which the Americans had rejected or felt had already been agreed to in the April framework.

“Kerry really just lost it,” the official said. “Let me rephrase: Kerry was adamant that this would be a non­starter for us, a non­starter for our partners, and it was just not possible to revisit some of these things that had been agreed.”

As their voices rose, the argument was audible to members of the French delegation dining next door. A Kerry aide slipped into the room to ask them to keep it down.

The next evening, the full group of ministers met with the Iranians to present a proposal that packaged several outstanding issues, thinking it would be more efficient than discussing them piecemeal. This time, Zarif blew up. “Don’t threaten an Iranian,” he warned.

Russian Foreign Minister Sergei Lavrov, trying to relieve the tension, joked, “Or a Russian.” It’s an offer, he told Zarif, not an ultimatum.

“It led some of us to be concerned that maybe there was something else going on here,” an official said, “that maybe he was starting to conclude that it wasn’t going to be possible to get to yes on a deal, or maybe his guidance from his superiors had been constrained.”

If you don’t think you’re ready to make a deal, Kerry told Zarif, he was “ready to go home.”

After more than two hours, the meeting adjourned. The P5+1 then met alone in one of the dining rooms, without staff, to assess where they were. It was the lowest point yet, and they went to bed not knowing whether it was the end.

But the next morning, Tuesday, July 7, Zarif appeared at a one-on-one meeting with Kerry with a substantive response to the proposal.

“Whatever it was the night before,” the official said, “that moment had passed, and we were now back to talking substance.”

The main sticking points

A secure video conference between President Obama and the U.S. team had been set for Wednesday night, July 8, in Vienna. The U.S. delegation huddled around a screen in the secure tent that had been set up inside a Coburg hotel room.

The president went through the framework and was assured that nearly all items agreed to in April had been firmed up.

“Everything was pretty much set and cooked,” a senior administration official in Washington recalled of the pending deal. The main sticking points, he said, were the duration of a U.N. Security Council resolution that would bless the deal and set its limits, and the inclusion in it of existing embargoes on Iranian sales and purchases of conventional weapons and ballistic missile technology.

The United States — concerned about Iran’s arms shipments to Syria and to groups in Lebanon, Yemen and beyond, and determined that Iran would not be able to build a long-range missile — wanted to retain the embargoes for as long as possible.

Iran had long argued that because the embargoes were attached to nuclear-related sanctions, they should be lifted along with other restrictive measures as part of the nuclear deal.

The issue was further complicated by divisions within the P5+1. Russia and China, eager to sell weapons to Iran, took Tehran’s side.

Obama told them that without a satisfactory resolution, there would be no deal, according to U.S. officials in Vienna and Washington. He outlined the parameters he was willing to accept.

The congressional deadline was not an issue, they agreed. The separate deadline, when the interim restrictions on Iran’s nuclear program would expire, could be extended again.

As they neared a deal over the weekend, Kerry wanted to get one thing straight with Zarif. “Are you authorized to actually make a deal, not just by the [Iranian] president, but by the supreme leader,” Kerry asked, referring to Ayatollah Ali Khamenei. Zarif assured Kerry that he was, according to a U.S. official.

By Sunday, virtually all issues had been settled except for disagreements about the embargo and resolution durations. Kerry went to Mass at St. Stephen’s Cathedral in Vienna.

Late Sunday and Monday, foreign ministers who had left for the weekend returned for non­stop meetings.

At 10:30 p.m. Monday, Kerry was meeting in his suite with Lavrov and E.U. foreign policy chief Federica Mogherini when they were joined by Zarif. Staff members were asked to leave the room.

A half-hour later, the diplomats emerged to say they had resolved all major issues. The arms embargo would last for five years, a ban on missile technology would last for eight years. The U.N. resolution would continue for at least a decade.

At 11:50 p.m., Kerry spoke by telephone to Obama.

Early Tuesday, the ministers gathered.