By Paul Reynolds
World Affairs correspondent BBC News website
President Ahmadinejad announces his "present" to Britain
President Ahmadinejad announced the release of the 15 British naval personnel like a card player flinging down his hand to scoop the pool.
Iran had good cards and played them well.
It made its point about defending its borders, dominated international television with pictures of its prisoners and their "confessions" and, when it perhaps judged that it had got as much as it could expect to out of the confrontation, ended it with a flourish.
Iran will project this as a victory (the medals given publicly to the officers who led the operation was an immediate example) against a country still viewed with suspicion in Iran because of its past interventions.
It also put out an indirect warning that any attack on its nuclear plants would be met with vigour.
At the same time, the British government can argue that it managed to put enough pressure on Iran to force it to put an end to the confrontation without Britain having to make any formal statement, even of regret, at the incident.
Nor was there any linkage to anything else, notably five Iranian officials held in Iraq.
Britain gave Iran an initial period of quiet in which to release the prisoners and when that did not work, went to the UN and the European Union while rallying support in the region.
Whether that counted is not known. But certainly Iran complained about it, so it was noticed.
Even the Pope joined in the pleas, with references to the sailors being home for Easter, and that might have been designed to appeal to President Ahmadinejad's religious feelings.
The decisive moment came with the intervention of Ali Larijani, a senior figure on Iran's National Security Council and the lead negotiator on the nuclear issue, into what had been until then an inconclusive, drawn-out, formal diplomatic exchange through embassies.
He contacted Channel 4 News in London, whose staff met him during a visit to Iran a few months ago. He made it clear in an interview on Monday that Iran wanted a diplomatic solution.
It was diplomacy by media. The normal channels were not functioning, a lesson for the future.
The Larijani interview showed that the mood in Tehran had softened. The day before, hardliners organised a demonstration outside the British embassy.
It will probably be back to confrontation when the next Security Council deadline for Iranian compliance comes on 23 May.
His intervention indicated that pragmatists had prevailed. This was matched on the British side by a lowering of the temperature, which proved important as it allowed Iran to say that the British were being less arrogant.
When more photos of the prisoners appeared, showing them playing chess and sitting on the floor, chatting and smiling, the UK Foreign Office made no complaint, as it had after earlier pictures.
Channel 4 News presenter Jon Snow told British officials about the origin of the Larijani interview at a briefing for correspondents by the Foreign Secretary Margaret Beckett on Tuesday.
This appeared to galvanise British diplomats into contacting Mr Larijani themselves. That evening, Sir Nigel Sheinwald, Tony Blair's foreign affairs private secretary, spoke to Mr Larijani by phone.
President Ahmadinejad's news conference had been postponed from the previous day so presumably a collective decision was taken during the delay.
The president told the BBC's Frances Harrison that he had not wanted a confrontation and suggested that the release had been held up by the British attitude.
In any event, he announced the release with some enthusiasm.
One lesson that the British and other Western governments will draw is that Iran cannot be given opportunities like this without a propaganda price being paid.
It appeared to observers during the crisis that the Royal Navy adopted the attitude that it was right and that was all there was to it.
The 15 sailors met Mr Ahmadinejad dressed in smart suits
The navy's confidence was reflected in the fact that the helicopter that was monitoring the ship search preceding the seizure went back to HMS Cornwall without remaining overhead.
Yet this, by British calculation, was within three minutes of the arrival of more powerful Iran boats. It is unlikely that there will be such confidence again.
And the incident was set against a background of hostility towards Iran by the British and American forces in Iraq.
They have accused the Iranian Revolutionary Guards, whose naval unit took the British party, of helping Shia militias in their attacks on foreign troops. This incident is a reminder that all forces operate in a political context.
The Iranian decision in this case is unlikely to be reflected in the much larger issue of its argument with the Security Council over its enrichment of uranium.
It was noticeable that President Ahmadinejad spent much of his statement to the news conference denouncing the Security Council for being the instrument of the United States.
He accused the West of trying to deny "scientific knowledge" to Iran, even though it is simply the enrichment process that is in question.
His attitude indicates that it will probably be back to confrontation when the next Security Council deadline for Iranian compliance comes on 23 May.