Peter Moore, the only known survivor from five British hostages in Iraq, has been released in Baghdad.

The computer programmer was captured in the Iraqi capital in 2007 by Shia militants with four other Britons and spent 31 months in captivity. The bodies of all but one of the others have since been returned to Britain.

Moore, aged 39, was released to Iraqi officials this morning and then transferred to the British embassy in Baghdad. A young Shia cleric, Qais al-Ghazali, held by the US military, was being handed over for release to the Iraqi government in exchange, officials said.

The British foreign secretary, David Milliband, said Moore was in good health and "delighted" to be freed. He was in a "remarkable frame of mind given the two and a half years that he has had. Real strength, real commitment, a real determination to get back to his life."

Miilband said the British government had made no substantive concessions to the kidnappers and attributed Moore's release to the process of reconciliation being carried out by the Iraqi prime minister, Nouri al-Maliki. "The process of reconciliation is the foundation of the decision of the kidnappers to release him," Miliband said.

Moore's captors called the Iraqi government early this morning and said they were willing to free him in east Baghdad. The hardline Islamic group known as the Righteous League had insisted on trading Moore for Ghazali, who was captured near Basra by the SAS along with his brother Laith al-Ghazali and a senior member of Lebanese Hezbollah in March 2007.

Foreign secretary's statement on release of IT worker taken hostage with four other Britons in Baghdad two years ago Press Association

The others Britons captured with Moore – all security guards – were Alec Maclachlan from Llanelli in Wales, Alan McMenemy from Dumbarton in Scotland, Jason Swindlehurst and Jason Creswell. The bodies of Swindlehurst and Creswell were identified in June, followed by Maclachlan in September. McMenemy is also believed dead, though his body has not been returned.

Until today there had been no word on Moore's fate since a DVD was handed to Iraqi officials a few months ago showing him alive.

Senior Righteous League members have said Moore did not know the fate of the other four, all of whom were almost certainly killed about 18 months ago, according to forensic science findings.

Moore's father, Graeme, 60, from Wigston, Leicestershire, said he was "over the moon" at the news. "We are so relieved and we just want to get him home, back now to his family and friends. I'm breaking down, I'm just so overjoyed for the lad. It's been such a long haul. I know that there have been one or two people working in the background to get Peter released.

"Peter is a very resilient lad and he always has been because of his background … but I don't know how close he was to those others who have been shot."

Graeme Moore said he felt the Foreign Office had been "obstructive" in the effort to secure his son's safe release.

Gordon Brown said he was "hugely relieved" by the "wonderful news" that Moore had been released and would be reunited with his family. "They have faced a terrible ordeal and I know that the whole nation will share their joy that he is coming home," Brown said. "At this moment of celebration we also remember the families of British hostages who have been killed in Iraq and elsewhere."

There has been a phased release of Shia prisoners from American custody this year, many of whom had been linked to the Righteous League and to the firebrand cleric Moqtada al-Sadr.

Moore's release had been widely predicted to take place during the Islamic festival of Eid al-Adha, which this year fell in November. As it drew near Moore's captors had grown increasingly concerned about his psychological state. The net of those who knew his whereabouts was so small that the kidnappers would not risk taking him to a doctor to treat a range of ailments he developed over the past six months.

"To an extent the captors had become captive to their hostage," said one senior Iraqi government negotiator familiar with the negotiations. "They wanted to hand him back but they were not going to do so before Sheikh Qais was freed. And the Americans were holding out."

Moore is being debriefed in Baghdad by embassy officials, medical staff and police attached to the embassy. He is expected to be flown to Kuwait or Jordan and then home to Britain. He had been working in Iraq as an IT consultant for the country's finance ministry, where he had also been helping install financial software. The five Britons were kidnapped by up to 40 men, many of whom were dressed in Iraqi police uniform, and driven towards Sadr City.

Earlier this month a leading member of the Righteous League confirmed for the first time to the Guardian that the group was holding Moore and expressed frustration at delays over the trade for Ghazali.

"This was due to be completed at the time of Eid al-Adha," said Salam al-Maliki, a Shia politician who heads the Righteous League's dialogue committee. "It was not finalised because the Americans did not honour their pledge to release Sheikh Qais.

"We have discussed this for many months and there has been progress made. But the sticking point has always been Sheikh Qais."

A blueprint for the release of Moore was outlined earlier in the year in an interview given by a senior Righteous League figure to a Saudi website. The interview spelled out a phased series of trades of the five Britons for a large number of Shia Islamist prisoners, culminating in a swap of Moore for Ghazali and Ali Mahmoud al-Dakduk, of Hezbollah.

The US military blames Ghazali, his brother Laith and Dakduk for an ambush in the Shia shrine city of Karbala in January 2007 in which five US soldiers were killed. The three were captured three months later by the SAS near Basra. Moore and the four guards were seized in Baghdad in late May the same year to try to secure the militants' release.

A promising political career beckons for Ghazali, who has been courted by several Shia factions for a senior role. The former understudy to Moqtada al-Sadr has won favour in Shia Islamic circles in neighbouring Iran.

The British government has been repeatedly criticised for its hands-off role in the hostage negotiations – it had aimed not to legitimise hostage-taking as a political tactic in Iraq. The recently departed ambassador to Iraq, Christopher Prentice, defended the government's stance. Days before leaving Baghdad he said: "It was the proper approach and offered the best chances of making progress.

"The way forward has always been through engagement with the government of Iraq and they have been very supportive, and in my final meeting with the prime minister we again discussed this and he repeated his commitment to try to bring this to a conclusion. The best opportunity has been to try to persuade the hostage-holders to do the proper and right thing to release [them … through a process of broader national reconciliation]. That is still the best framework for this."