TEHRAN - Iran on Wednesday released 10 US sailors it had detained in the Gulf, moving within hours to head off a potential crisis as it prepares for the lifting of sanctions.

A dramatic series of events started with the sailors - nine men and a woman - being taken into custody after their two patrol boats drifted into Iranian territory late on Tuesday.

US and Iranian officials scrambled to defuse the situation, which unfolded as Iran prepares to finally implement a nuclear deal with world powers aimed at ending the Islamic republic's long international isolation.

After informal talks between Washington and Tehran, a statement from Iran's Revolutionary Guards, describing the sailors as Marines, was read out on state television confirming they were free. "It was determined that the detained American Marines did not enter Iranian waters intentionally. Following their apology, they have been released to international waters in the Gulf," it said. Still images used in the report showed the sailors sitting calmly on Persian rugs. Iran had earlier said they were being well treated.

Pictures of the US boats, which had green-and-black camouflage patterns, were also shown.

A Pentagon statement confirmed the sailors were free. "There are no indications that the sailors were harmed during their brief detention," it said, adding: "The Navy will investigate the circumstances that led to the sailors' presence in Iran."

Admiral Ali Fadavi, the naval commander of the Guards, said an investigation established that "this trespassing was not hostile or for spying purposes" and the sailors had entered Iranian territory "due to a broken navigation system".

US officials had said one or both of the boats had suffered mechanical problems and been taken to Farsi Island, which lies roughly midway between Iran and Saudi Arabia in the Gulf and houses a base of the Guards, which has its own naval units.

Radio contact was lost with the two vessels - which US officials said were riverine patrol boats under 65 feet (20 meters) in length - while they were en route from Kuwait to Bahrain.

American officials said they had received assurances from Iran that the crews would be allowed to sail onwards come first light.

Washington and Tehran have no diplomatic relations but US Secretary of State John Kerry called Iranian counterpart Foreign Minister Mohammad Javad Zarif to discuss the incident.

The two developed a close working relationship during the nuclear talks, which concluded in July with a deal between Iran and the so-called P5+1 powers of the United States, Britain, China, France, Russia and Germany. But the incident in the Gulf came as President Barack Obama gave his last State of the Union address, undermining any attempt to cite closer relations with Tehran as part of his legacy.

The nuclear accord foresees Iran scaling back its activities to put an atomic bomb outside its reach in exchange for relief from crippling international sanctions.

The deal is to be implemented very soon - Kerry has said "in the coming days" - but has been criticised by Obama's US opponents as too soft on Tehran.

These rivals seized on Tuesday's incident in the Gulf to hammer on this point, demanding Obama make a statement and warning Iran must release the sailors. "Iran is testing the boundaries of this administration's resolve," Republican presidential hopeful Marco Rubio said.

Following the release of the US sailors, the head of Iran's armed forces hit back at critics, saying Iran had shown "wisdom and goodwill" during the incident.

"Those US Congressional lawmakers that come up with a new obstacle for Iran every day don't seem to have correct information," General Hassan Firouzabadi said. "I hope that the incident... which probably won't be the last mistake from American forces in the region, will be a lesson for stonewallers in the US Congress."

Iran's Guards often take a tough approach to perceived and real territorial violations in what it considers the "Persian Gulf".

Relations with Washington were strained by claims last month that Iran fired missiles close to a US aircraft carrier in the Gulf.

Last year, Iranian patrol boats seized the Maersk Tigris, a cargo ship sailing under the Marshall Islands flag, which meant it was under US protection.

And in March 2007, Iranian patrols captured 15 British Royal Navy and Royal Marines personnel, interrogated them and held them for 13 days before releasing them.