A short time after the conference ended, television coverage showed several of the sailors speaking with the president and one man could be heard telling him: "We appreciate it. Your people have been really kind to us, and we appreciate it very much."
One the other service members was heard saying in English: "We are grateful for your forgiveness."
Ahmadinejad responded to him in Farsi: "You are welcome."
An Iranian official said the 15 sailors were to return home to Britain on Thursday.
Tony Blair, the British prime minister, welcomed the news of the release.
He said Britain bore no ill will toward the Iranian people.
"I'm glad that our 15 service personnel have been released and I know their release will come as a relief not just to them but to their families," Blair said.
"Throughout, we have taken a measured approach, firm but calm, not negotiating but not confronting, either.
"To the Iranian people I would simply say this. We bear you no ill will."
The news of the release of their relatives was met with joy by the families of the captured service members.
"It's been a long, long 13 days," Nick Summers, brother of captive Nathan Summers, told Sky News after the unexpected announcement by Ahmadinejad.
The family of Royal Marine Adam Sperry said the announcement was "the best present imaginable.
"The one thing I wanted was Adam's safe return to his family for Easter," said his uncle Ray Cooper. "Whoever has been in the right or wrong, the whole thing has been a political mess, so let's just get them home. It's great."
A spokesman for George Bush, the US president, also hailed the news.
The announcement of the release took most people present at the news conference by surprise.
Ahmadinejad had spoken at length about various historical matters and criticised Britain’s attitude to the diplomatic row before pausing to award the medal of honour to the guards who captured the sailors on March 23.
|Ahmadinejad awarded the medal of honour to |
the guards who captured the sailors [Reuters]
He then announced "although Iran has the right to prosecute them by following the model of the prophet the 15 people were pardoned and their freedom given as a gift to the British people."
He said the Britons, some of whom had been paraded several times on state television "confessing" to trespassing in Iranian waters, "will be going back home today."
He also said the British government had promised in a letter not to repeat the incident.
"The Blair government chose the path of media hype and sent the issue to the UN Security Council," Ahmadinejad said. "The British people can ask its government what the British soldiers are doing in Iraq or in Iranian waters," he said.
According to Ali-Reza Ronaghi, Al Jazeera’s correspondent at the conference, the decision to wait until the very end of the news conference before making the dramatic announcement was typical of Ahmadinejad.
"It is very typical of him [Ahmadinejad], 40 minutes of history then cut through at the end just when people are making their verdict," he said.
|The 15 navy personnel have been consistently |
shown on Iranian television [Reuters]
It was unclear whether the decision to release the sailors was taken at the behest of Ahmadinejad himself or whether it was taken with the approval of Iran's religious leaders.
The decision to release the detainees came after Iran had earlier acknowledged a "change of tone" from Britain following talks between Ali Larijani, the secretary of Iran's Supreme National Security Council and Sir Nigel Sheinwald, Blair's chief foreign policy advisor, late on Tuesday.
Iran had insisted that they key to resolving the crisis was an admission from Britain that the sailors and marines did intrude into Iranian territorial waters when they were seized on March 23.
Britain maintains the group was carrying out routine anti-smuggling operations in Iraqi waters in line with a UN mandate, but Iran says the sailors' Global Positioning System (GPS) devices show they intruded on Iranian waters.
The latest developments coincided with the release in Baghdad of an Iranian diplomat kidnapped in Iraq in early February. Iran had blamed US forces in the country for the abduction.
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.