Iran's Islamic Revolutionary Guard Corps (IRGC) said it had released the sailors after determining they had entered Iranian territorial waters by mistake. IRGC Rear Admiral Ali Fadavi said the boats had strayed because of a broken navigation system.
There were no indications the sailors were harmed while in Iranian custody, the Pentagon said. The quick resolution contrasted with previous cases in which British servicemen were held considerably longer, in one case nearly two weeks.
Iran expects the U.N. nuclear watchdog to confirm on Friday it has curtailed its nuclear programme, paving the way for the unfreezing of billions of dollars of Iranian assets and an end to bans that have crippled oil exports.
In a statement carried by state television, the IRGC said the sailors were released in international waters after they apologised.
Iranian state television later released footage of one of the detained men, identified as a U.S. Navy commander, apologising for the incident.
"It was a mistake, that was our fault, and we apologise for our mistake," the sailor said on IRIB state TV.
A Pentagon spokesman, Commander Gary Ross, said the video appeared to be authentic, "but we cannot speak to the conditions of the situation or what the crew was experiencing at the time."
A U.S. defence official, speaking on condition of anonymity, said: "Clearly this staged video exhibits a sailor making an apology in an unknown context as an effort to defuse a tense situation and protect his crew."
U.S. Vice President Joe Biden earlier rejected reports that Washington itself had offered Iran an apology over the incident.
The incident spilled over into the U.S. presidential campaign, where Republicans are critical of the nuclear deal Iran forged with six world powers and due to take effect soon.
At a campaign rally, Republican presidential front-runner Donald Trump, who accuses President Barack Obama of being weak on foreign policy, described the detention of the sailors as "an indication of where the hell we're going".
A carefully worded Pentagon statement did not explain how the sailors and their two riverine command boats ended up being detained by Iran, saying only that "the Navy will investigate the circumstances that led to the sailors' presence in Iran".
The sailors were later taken ashore by U.S. Navy aircraft, while other sailors took charge of the boats and headed towards Bahrain, their original destination.
A senior U.S. defence official said the circumstances surrounding the incident were still not entirely clear. "We haven't been able to fully debrief the sailors," the official said.
The sailors were headed to a U.S. military facility in Qatar. Another defence official said the debriefings would begin on Thursday after medical and psychological tests of the sailors on Wednesday.
U.S. Defense Secretary Ash Carter said he appreciated "the timely way in which this situation was resolved". He thanked Secretary of State John Kerry for engaging with Iran to secure the sailors' swift return.
Kerry spoke to Iranian Foreign Minister Mohammad Javad Zarif at least five times by phone, U.S. and 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. He repeated that message in follow-up calls, the official 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.
Zarif said on Twitter that he was "happy to see dialogue and respect, not threats and impetuousness, swiftly resolved the sailors episode".
Kerry thanked Iran.
"I think we can all imagine how a similar situation might have played out three or four years ago, and the fact that today this kind of issue can be resolved peacefully and efficiently is a testament to the critical role diplomacy plays in keeping our country safe, secure, and strong," Kerry said.
Four photographs published by Shargh Daily, a Tehran newspaper, and posted on Twitter, purportedly show the moments after one of the U.S. boats was stopped by the IRGC.
Iranian state television released footage of the arrest, showing the sailors as they knelt down with hands behind their heads and their two vessels being surrounded by several IRGC fast boats.
The video showed weapons and ammunition confiscated from the sailors, who were seen eating food provided by the Iranians. There were also images of American passports being inspected.
The incident raised tensions between Iran and the United States, which, along with other world powers, reached the deal under which Iran will curb its nuclear activities in exchange for the lifting of economic sanctions.
Some conservatives in both countries, enemies since Iran's 1979 Islamic revolution, have criticised the deal that is due to be implemented in the coming days.
Iran's armed forces chief, Major General Hassan Firouzabadi, said the incident should demonstrate Iranian strength to "troublemakers" in the U.S. Congress, which has sought to put pressure on Iran after the nuclear deal.
Attributing the boats' incursion into Iranian waters to a navigation error marked a de-escalation in rhetoric. Earlier, the Guards had said the boats were "snooping" in Iranian territory and Zarif had demanded an apology from Washington.
The IRGC, the Islamic Republic's praetorian guard, is highly suspicious of U.S. military activity near Iran's borders and many senior officers suspect Washington of pursuing regime change in Tehran.
The Guards operate land and naval units separate from the regular armed forces and stage frequent war games in the Gulf, which separates Iran from its regional rival Saudi Arabia and a U.S. naval base in Bahrain.
Last month, the U.S. Navy said an IRGC vessel fired unguided rockets near the aircraft carrier USS Harry S. Truman in the Strait of Hormuz, a critical shipping route for crude oil that connects the Gulf to the Indian Ocean. Iran denied the vessel had done so.
In April 2015, the Guards seized a container ship belonging to Maersk, one of the world's major shipping lines, in the Gulf because of a legal dispute between the company and Iran. The ship and its 24 crew members were released after 10 days.
(Reporting by Bozorgmehr Sharafedin, Susan Heavey, Phil Stewart, Jonathan Landay, Andrea Shalal, Warren Strobel and Matt Spetalnick; Writing by Sam Wilkin and Peter Cooney; Editing by Howard Goller and Sandra Maler)