"After the news conference they can go to the airport and go back home. They will be going back home today."
He told reporters that Britain was wrong to take the dispute to the UN and awarded the medal of honour to the military commander in charge of the forces who arrested the sailors.
A spokesman for Tony Blair, the British prime minister, said Downing Street "welcomed" the news.
A British embassy spokesman in Tehran declined to comment on whether the announcement had taken London by surprise. "We are following up the details now," he said.
The families of the captured marines and sailors also welcomed the news.
"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.According to Ali-Reza Ronaghi, Al Jazeera’s correspondent at the conference, the decision to wait until the very end of the press conference before making the dramatic announcement was typical of Ahmadinejad.
"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 host of the news conference said Iran was holding a ceremony on Wednesday to mark the release of the sailors and that reporters could attend.
"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.
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.
Iranian political analysts said the release was the logical conclusion to the diplomatic standoff.
"It was generally expected in the sense there was no further mileage to be had for the Iranian government by holding onto these sailors,” Dr Mehrdad Konsari of the Centre for Arab and Iranian studies in London told Al jazeera.
"The megaphone phase of diplomacy was over and quiet diplomacy could begin. This is why you saw the entrance [into negotiations] of Larijani [Ali Larijani, the secretary of Iran's Supreme National Security Council] in recent days."