In a surprise announcement during a news conference at the presidential palace in Tehran, Mr Ahmadinejad said the 14 men and one woman would be "going back home" in a move marking the birthday of the prophet Muhammad last Saturday and acknowledging Easter.

Iran's IRNA state news agency said later the Britons would leave Tehran tomorrow at 8am (5.30am UK time) on a flight to London.

Immediately after the news conference, Iranian television showed footage of the naval crew effusively thanking Mr Ahmadinejad on the steps of the palace.

Dressed in grey suits, apart from the sole female captive, Leading Seaman Faye Turney, who was wearing a striped top and a headscarf, they appeared delighted. "We are very grateful for your forgiveness," said one of the male captives, Lieutenant Felix Carman. "You are welcome," Mr Ahmadinejad responded in Farsi.

"You came here on a compulsory trip," the Iranian leader told another, getting the answer: "I don't know if I'd put it like that but you could call it that."

Tony Blair welcomed the news, calling the crew's imminent freedom "a profound relief not just to them but to their families, that have endured such distress and anxiety over these past 12 days".

Britain had taken "a measured approach, firm but calm, not negotiating but not confronting either", he said, adding in a message to the Iranian people that he hoped any other disagreements could be resolved peacefully.

Relatives of the detainees reacted with uncontained delight. Sandra Sperry, the mother of Royal Marine Adam Sperry, told Sky News: "I'm absolutely ecstatic ... I heard in Asda ... I think everyone thought I had gone mad ... we thought it would drag into next week."

It had been feared that Mr Ahmadinejad - renowned for being a hardliner and a critic of the west - would use his news conference to make more demands relating to the captives.

The initial signs were not positive: the president began with a long complaint about the invasion of Iraq, also criticising Britain for taking the case of the captives to the UN security council.

He also presented medals to three members of the Revolutionary Guard naval patrol that seized the Britons as they searched an Indian-registered merchant ship just outside the mouth of the Shatt al-Arab waterway, which divides Iran and Iraq.

But in a dramatic flourish about an hour into the address, the president suddenly announced: "While insisting on our rights, these 15 sailors have been pardoned and we offer their freedom to the British people."

Iran repeatedly said the patrol had been in Iranian waters, but Britain presented GPS evidence last week that it said proved the UK personnel had been well inside the Iraqi zone.

Later on during his two hour-plus press conference, Mr Ahmadinejad said the British government had sent a letter to Iran's foreign ministry pledging that incursions "will not happen again".

Iranian TV has shown a series of broadcasts in which some of the detainees have apparently admitted being in Iranian water, prompting an angry reaction from British officials.

In a seemingly mischievous aside during his press conference, Mr Ahmadinejad said he hoped this would not get them into trouble, saying: "I ask Mr Blair not to punish the soldiers with the charge of accepting and telling the truth."

There has been speculation that the release was prompted in part by an agreement to let an Iranian representative meet five Iranians detained by US forces in Irbil, northern Iraq, in January.

Tehran says the men are diplomats; the US says they are Revolutionary Guards linked to insurgents in Iraq. There have been claims that Tehran orchestrated the seizure of the British crew with a view to an exchange.

The US insisted today that there was no connection between the cases. "Not that I am aware of," said a State Department spokesman, Tom Casey, when asked if there was a link.