U.S. sailors are seen in an undisclosed location in Iran in this handout picture released on Farsnews website on January 13, 2016.farsnews.com/Handout via Reuters

WASHINGTON (Reuters) - Iran's detention of 10 U.S. sailors on Tuesday set off a furious round of meetings in both Washington and Tehran. Anxious officials in both capitals had the same goal: to ensure the incident did not torpedo a historic nuclear accord between two countries with a long history of hostility.

In the end it came down to a series of telephone calls between U.S. Secretary of State John Kerry and his Iranian counterpart, Mohammad Javad Zarif, who had forged a close bond during months of tense negotiations on Iran's nuclear program.

As the Iranian and U.S. governments scrambled for details of the incident, the two men spoke at least five times by phone, U.S. and Iranian officials said. Iran's Supreme Leader, Ayatollah Ali Khamenei, ordered an immediate investigation to determine whether the U.S. sailors had deliberately sailed into Iranian waters, and ultimately had the final say in their release, Iranian officials said.

High-level gatherings in Washington were mirrored in Tehran, where top security and government officials held at least three meetings. Iranian President Hassan Rouhani attended the sessions, one Iranian official told Reuters.

"Of course, there were tense moments and ... various views over handling the case," a second Iranian official said.

The drama in the Gulf, which the U.S. government had initially hoped to keep under wraps, became public knowledge just hours before President Barack Obama was due to give his annual State of the Union address in Congress.

Kerry learned of the detention of the sailors in their two small craft at 12:30 p.m. EST, as he and Defense Secretary Ash Carter met with their Filipino counterparts on the State Department's eighth floor.

Kerry almost immediately excused himself and went to his seventh floor office. As it happened, he already had a call scheduled with Zarif at about 12.45 EST.

Appealing for the sailors' quick release, Kerry told Zarif: "We can make this into what will be a good story for both of us," according to a senior State Department official. He repeated that message in follow-up calls, the official said.

Looming large was the nuclear deal, which both men have invested so much in and striven to protect. In Washington, the deal has come under sustained attack from majority Republicans in Congress who have accused Obama of weakness and say the Iranians are not to be trusted.

In Tehran, the stakes were no less high. Formal implementation of the nuclear deal is expected to begin within days, giving Iran billions of dollars in relief from economic sanctions in return for curbs on its nuclear activities.

"There were some concerns, including political and military consequences of the issue, and the government officials were also worried about its affect on the nuclear deal and its implementation," one of the Iranian officials said.

Supreme Leader Khamenei "underlined the importance of showing Islamic clemency and good behavior to the detained soldiers, especially to the female sailor," the second official said.

"After making sure there were no evil intentions from the sailors' side, Leader Khamenei gave the green light for the release of sailors," the official said.

In an Iranian television interview, one of the sailors apologized and said the boats had mistakenly entered Iranian waters. The sailors were released from Farsi Island on Wednesday. U.S. officials said they were unharmed, and that they had made no government-to-government apology to Tehran.

JOHN AND JAVAD

It fell though to Kerry and Zarif to do the diplomatic heavy lifting.

Over months of negotiations and close contact, Kerry has developed a close working relationship with Zarif, a fluent English speaker who earned his doctorate at the University of Denver. They address each other by first names, John and Javad.

Kerry has credited Zarif with helping bridge the deep mistrust between the U.S. and Iranian governments after decades of estrangement and has remained in contact by phone with him in the months since the nuclear deal was negotiated.

"One thing that I can say with confidence is that this channel between Secretary Kerry and ... their foreign minister was very important in resolving this in a timely fashion," White House chief of staff Denis McDonough told reporters.

In an account that aligns with that from Iranian officials, the senior State Department official said Kerry spoke with Zarif by phone at 1 p.m., 2 p.m., 3:15 p.m. and 10:30 p.m. Washington time. The two had other, very brief phone chats as well, he said.

Kerry, a former Navy officer, pressed Zarif on the need for Iran to release the Americans "safely and unharmed and as quickly as possible," the senior State Department official said.

"At the front of mind for everybody, including I would suspect on the Iranian side, was the concern ... that there would be risk of escalation and the spillover of this issue into other issues, including no doubt the nuclear situation," the official said.

The nuclear deal and U.S.-Iran ties, such as they are, already had been rattled by other incidents in recent months.

In October and November, Iran launched ballistic missiles in violation of U.N. sanctions. After the latter test, the White House considered imposing fresh sanctions but decided to hold off.

One U.S. administration official said the move was made out of a desire "not to rock the boat" during a sensitive time as implementation of the nuclear deal approached.

Then, in late December, the United States said that Iranian vessels fired rockets close to the U.S. aircraft carrier Harry S Truman in the Gulf.

The breakthrough call between Zarif and Kerry came around 3:15 pm.

"At that point," the senior State Department official said, "Foreign Minister Zarif said to the secretary that he had indications from the people who were holding our sailors that they would be free to go at dawn, that it was probably not safe for them to transit during the course of the night in dark."

(This story has been refiled to replace "Iranians" with "Americans" in paragraph 22)