In a surprising twist, the Iranian president announced the pardon and release of 15 British naval personnel to a stunned news conference broadcast live around the world.
The dramatic announcement came from out of the blue
Just minutes later, some of the crew were shown on Iranian state television, wearing smart grey suits and no ties - in compliance with Islamic revolutionary dress codes - thanking President Mahmoud Ahmadinejad for their freedom.
President Ahmadinejad wished "good luck" to the Britons who had been detained for 13 days, at a ceremony in Tehran to mark their release.
He smiled, joking with one Briton: "How are you?... So you came on a mandatory vacation."
The British crew members, who had reportedly "shouted for joy" at the news of their release, lined up to shake the president's hand and exchange polite words.
"I would like to thank yourself and the Iranian people," said one.
"Your people have been very kind to us and I appreciate that very much," said another.
Mr Ahmadinejad responded in Farsi: "You are welcome."
President Ahmadinejad had twice cancelled news conferences in recent days. The start of Wednesday's event did not betray any hint of the drama that was to come.
It started with a long reading from the Koran marking the birthday of the Prophet Muhammad.
The president gave no hint of the drama that was about to unfold
It was more than an hour into the 90-minute statement before he even mentioned the issue of the British sailors and marines.
The president first awarded a medal to the naval commander responsible for the capture of the HMS Cornwall personnel, saying it represented the gratitude of the Iranian people.
At that point hopes for the Britons' release looked very dim.
Then in a theatrical flourish the president said he would, as a goodwill gesture, pardon the servicemen and set them free as "a gift to the British people".
Completely changing the tone of the news conference, Mr Ahmadinejad then basked in the moment to enjoy the shock effect of his announcement.
He said he was willing to forgive the sailors, even though Britain was not "brave enough" to admit it had made a mistake and strayed into Iran's territory.
The BBC's diplomatic correspondent Jonathan Marcus says the Iranian leadership had clearly decided it had made its point and extracted the maximum value from this affair.
Iran emerges in its own narrative as a staunch defender of its territorial integrity but magnanimous to those it sees as being caught up in the scheming of the big powers, our correspondent adds.
The British crew members were introduced to the president
Mr Ahmadinejad said he had asked UK Prime Minister Tony Blair "not to punish" the crew for
confessing they had been in Iranian waters when they were seized.
He referred to the footage broadcast by Iranian media of some of the crew making confessions.
The president said the captives would be released immediately. They are expected to arrive back in Britain on Thursday.
So a potentially embarrassing episode has been brought to an end and a long-drawn out crisis avoided.
What will now be under urgent review is the way in which patrol and boarding operations in the Gulf are conducted to make sure British servicemen and women will not be so vulnerable in future.