The release of the Royal Navy personnel will have come as a "profound relief" to their families, Tony Blair has said.
Tony Blair said there would be "relief" at the release of the crew
The prime minister said Britain had taken a "firm but calm" approach in negotiating with Iranian officials.
He said the UK bore "no ill will" to the Iranian people and wanted to resolve any differences with their government "peacefully".
The Tories and Lib Dems welcomed the freeing of the crew, but said questions would have to be asked in due course.
Speaking in Downing Street, Mr Blair said: "I'm glad that our 15 service personnel have been released.
"I know their release will come as a profound relief not just to them but to their families who have endured such distress and anxiety.
"Throughout we have taken a measured approach - firm but calm, not negotiating, but not confronting either."
Mr Ahmadinejad met members of the British crew
He added: "To the Iranian people I would simply say this: we bear you no ill will.
"On the contrary, we respect Iran as an ancient civilisation, as a nation with a proud and dignified history.
"The disagreements that we have with your government, we wish to resolve peacefully, through dialogue.
"I hope, as I have always hoped, that in the future we are about to do so."
Diplomatic sources have told the BBC Iran's announcement was "out of the blue" and a "real surprise".
An Iranian official in London said the sailors and marines would be handed over to British diplomats and that it would then be up to the Foreign Office to decide how they would return home.
"They will go through some brief formalities and then they will go to the embassy," the diplomat said.
Defence Secretary Des Browne followed Mr Blair in welcoming the breakthrough.
"I welcome this news," he said.
"It must be a tremendous relief for our people who have been held in Iran - they have acted with great dignity throughout.
"It is vital that we get them back home quickly and safely so they can be reunited with their families and loved ones - that is our priority now."
Tory leader David Cameron said the crisis meant lessons had to be learned for the future.
"It is very welcome news that our servicemen and women are going to be coming home," he said.
"They've had a very difficult time, their families have had an incredibly difficult time and I'm sure their families won't be happy until they walk through the door of home.
"Of course there'll be lessons to learn for the future - but I think today we should just welcome the fact they're coming home."
Shadow Foreign Secretary William Hague said: "We welcome the news that our navy personnel will be released.
"I'm sure their families and whole country are greatly relieved and we look forward to welcoming them home quickly and safely."
Shadow Defence Secretary Liam Fox said: "We all look forward to welcoming them home as soon as possible.
"There will be questions to be asked about this whole episode, but I think it would be prudent to restrict our comments and our questions until all our personnel are back in the UK."
Liberal Democrat leader Menzies Campbell said: "There will be many lessons to be learned from this episode.
"But for the moment we should welcome this announcement and look forward to the return of the naval personnel.
"Their seizure underlines the difficult and dangerous tasks we ask of our armed forces."