Iran's detention of 10 US sailors on Tuesday set off a furious round of meetings in both Washington and Tehran.
In the end it came down to a series of telephone calls between US 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.
Iran's Supreme Leader, Ayatollah Ali Khamenei, ordered an immediate investigation to determine whether the US sailors had deliberately sailed into Iranian waters, and ultimately had the final say in their release, Iranian officials said.
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.
The drama in the Gulf, which the US 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.