Britain, which said its crew was in Iraqi waters when seized, said it never offered a quid pro quo, but relied on quiet diplomacy.
"It is Iran's responsibility to find a quick solution"
Emkay, Auckland, New Zealand
Send us your views
Tony Blair, the British prime minister, said London's "measured approach" had been effective.
"Throughout we have taken a measured approach - firm but calm, not negotiating, but not confronting either," adding a message to the Iranian people that "we bear you no ill will."
However, Iran's announcement coincided with the release in Baghdad of an Iranian diplomat seized in Iraq in early February.
Iran had blamed US forces for the abduction but the US denied involvement.
Iranian state media also said five Iranian officials captured by US forces in northern Iraq in January and accused of seeking to stir trouble were expected to receive their first visit by an Iranian diplomat soon.
James Dobbins, a former Bush administration envoy to Afghanistan, said: "It allowed the Iranians to demonstrate that they can't be trifled with. They have a capacity to take action, and that will undoubtedly make people more careful."
The US cautiously welcomed Iran's announcement, although Dick Cheney, the vice-president, said "it was unfortunate that they were ever taken in the first place".
He said he hoped there would be no "quid pro quo" for their release.
But what Iran might have won is a more careful Western approach to the Middle East power.
The US says its policy is to arrest Iranians in Iraq who are funnelling arms or money to Shia fighters there. But it may be more cautious if it thinks Iran is willing to retaliate by seizing US troops.
Iran may also have been trying to moderate Ahmadinejad's hardline reputation, allowing him to announce the release to appear benevolent.
Or, it might have aimed to simply show that it can compromise, which may help it in its dispute with the US over its nuclear programme.
But some analysts said Iran's actions had caused it to be distrusted more by the international community, even if Tehran may have scored a slight propaganda victory at home.
Ahmadinejad said the British government was "not brave enough" to admit the crew had been in Iranian waters when it was captured.
But he said Britain had sent a letter to the Iranian foreign ministry pledging that the incident "will not happen again".
Britain's foreign ministry would not give details about the letter but said its position was clear that the detained crew had been in Iraqi waters.
Ahmadinejad declared that even though Iran had the right to put the Britons on trial, he had "pardoned" them to mark the birthday of the Prophet Muhammad, celebrated on March 30 this year, and the coming Easter holiday.
"This pardon is a gift to the British people," he said.
After the news conference, Iranian television showed a beaming Ahmadinejad on the steps of the presidential palace shaking hands with the Britons decked out in business suits and Leading Seaman Faye Turney, the only female crew member, wearing an Islamic head scarf.
One of the British men told Ahmadinejad: "Your people have been really kind to us, and we appreciate it very much." Another said: "We are grateful for your forgiveness."
Ahmadinejad responded in Farsi: "You are welcome."
But it was not just Iran and Britain who were trying to come off looking good in this standoff.
The president gave the coast guard commander
a medal for the capture of the Britons [Reuters]
Syria, Iran's close ally, said it had played a role in winning the release of the sailors.
Walid al-Moallem, Syria's foreign minister, said in Damascus on Wednesday: "Syria exercised a sort of quiet diplomacy to solve this problem and encourage dialogue between the two parties."
The breakthrough appeared to have caught the British government by surprise.
On Tuesday, Margaret Beckett, the foreign secretary, had told reporters not to expect a quick end to the standoff.
Some analysts say Iran's supreme leader, Ayatollah Ali Khamenei, decided the crisis had gone far enough at a time when Tehran faces mounting pressure over its nuclear programme.
Patrick Clawson, deputy director for research at The Washington Institute for Near East Policy, explained: "The thing ... about Iran's negotiating strategy is that they say, 'No, no, no' until it suddenly becomes 'Yes'."
Whether that is a sign of internal dissent in Iran or finely honed, clever brinkmanship, Iran clearly gained some things from the dispute - at least enough to make the West cautious that it would be willing to enter into such a standoff again.
Observers fear that the 13-day crisis may be precursor of things to come in Iran's confrontations with the West.