Protesters angry about the occupation of Iraq tried to approach the six Royal Marines and two sailors as they arrived at Tehran's airport accompanied by British consular officers, but they were kept away by police.
The men were taken to the British Embassy.
The eight were detained Monday after their boats apparently strayed into the Iranian side of the Shatt al-Arab waterway, or Arvand River, that runs along the Iran-Iraq border while delivering a patrol boat to Iraq's new river police.
"I'm told that they are in very good spirits and were well cared for," British Foreign Secretary Jack Straw said in a brief statement. "I'm obviously very pleased indeed, as, I know, their families and service colleagues will be, that they are now in British care."
The father of one of the men told the BBC he was "absolutely delighted" they had been released.
"It's the news we've been waiting for," said Graham Reid, whose 24-year-old son David was among those detained. "There were some dark moments over the last few days, especially when we saw them on TV blindfolded, but this is brilliant."
It's not known if the sailors will go back on duty in Iraq or head home to Britain, reports CBS News Correspondent Steve Holt.
Iran also briefly detained, investigated and then released an unspecified number of Turkish troops who had strayed across the border "by mistake," state-run television reported Thursday.
It gave no timing for the detention or release of the troops, but unconfirmed reports said Wednesday that 25 Turkish soldiers chasing Kurdish rebels crossed into Iran and were detained by Iranian forces.
The Arabic-language Al-Alam television station said a number of protesters waited at the Tehran airport for the arrival of the British servicemen. Hard-line Iranians opposed to Britain's prominent role in the occupation of Iraq have in recent weeks held angry demonstrations outside the British Embassy.
Strains between the two nations rose last week when Britain helped draft an International Atomic Energy Agency resolution for past nuclear cover-ups.
The servicemen's capture had further fueled tensions between the two countries, but Straw said he remained convinced that Britain's policy of engaging with Iran was wise.
"We have diplomatic relations with Iran, we work hard on those relationships and sometimes the relationships are complicated but I'm in no doubt at all that our policy of engagement with the government of Iran ... is the best approach," he said.
He praised the efforts of his Iranian counterpart, Kamal Kharrazi.
Iran had initially said it would prosecute the British servicemen for illegally entering Iranian waters. Concern in Britain ran high after Al-Alam television showed the sailors blindfolded and sitting cross-legged on the ground.
But telephone conversations between Straw and Kharrazi and constant dialogue between British and Iranian officials appeared to ease the situation, and Iran softened its position, saying the servicemen would be freed if interrogations proved they had "no bad intention."
Reza Pahlavi, the son of the ousted Shah, told British media he thinks the Iranian government has betrayed its weakness with this incident.
"Why would the regime do that, when they don't need additional headaches?" Pahlavi said. "If a regime feels confident, it will not resort to this kind of shenanigans."
Two of the detained men appeared on Iranian television Tuesday night apologizing for mistakenly entering Iranian waters.
The Ministry of Defense said the personnel were from the Royal Navy training team based in southern Iraq and were delivering a boat from Umm Qasr to Basra, Iraq, when they were captured.
Iran said it will keep the three boats in which the British troops were traveling, as well as their weapons and other equipment, Al-Alam reported. But Straw said Britain and Iran were still discussing the possible return of the sailors' equipment and boats.
The waterway has long been a source of tension between Iran and Iraq. The 1980-88 Iran-Iraq war broke out after Saddam Hussein claimed the entire waterway.