Sakran, Turkey - A Turkish court on Friday ordered the release of American pastor Andrew Brunson from house arrest, a move that will end his 24-month imprisonment and allow him to fly home, and that signalled a truce of sorts in a heated diplomatic dispute between Turkey and the United States.

Brunson was sentenced to three years, one month and 15 days in prison, but the judge lifted all judicial controls - including a ban on travel - making him free to leave the country immediately, because of a reduction for good behaviour and in view of time served.

He left the courthouse by car shortly after the decision was announced, and US officials following the case said he would return home to Izmir before departing for the United States on Saturday morning.

The Trump administration had pressed hard for the release of Brunson, an evangelical pastor who runs the small Resurrection Church in Izmir. He was one of two dozen Americans detained in the aftermath of a failed coup in 2016 and was charged with aiding terrorist groups and espionage, charges he denies.

Brunson’s prolonged detention and trial significantly raised tensions between the United States and Turkey, with President Donald Trump and Vice President Mike Pence personally raising his case several times with President Recep Tayyip Erdogan of Turkey, the United States imposing financial sanctions, and members of Congress traveling to Turkey to attend his trial.

“Thanks be to God,” said the Rev. William Devlin of New York, who has attended every hearing. “Pastor Brunson is going home. We thank the court, we thank Turkey and we thank President Erdogan.”

Washington and Turkey have been involved in complex negotiations over the fate of Brunson for months. Turkey is grappling with a growing economic crisis and has been anxious to reduce a fine of billions of dollars that the US Treasury is expected to impose on the state-owned Turkish bank, Halkbank, for its part in a conspiracy to violate US sanctions against Iran.

Brunson’s release also coincided with the disappearance of Jamal Khashoggi, a Saudi dissident and journalist who was a columnist for The Washington Post, inside the Saudi consulate in Istanbul.

Washington has accused Ankara of holding Brunson, along with roughly 20 Turkish-Americans and three Turkish employees of the US consular mission in Turkey, saying that there was no credible evidence and that the detainees were being used as leverage in the various disputes with the United States.

In particular, Turkey has requested the extradition from the United States of preacher Fethullah Gulen, whom it accuses of running a terrorist organisation and of instigating the 2016 coup attempt. Erdogan once suggested a swap of the cleric and the pastor.

Erdogan has also sought to reduce penalties against Halkbank. A bank official, Mehmet Hakan Atilla, was sentenced to 32 months imprisonment in May in a Manhattan court for his part in the scheme.

The two countries came close to agreeing in July to a coordinated release of Brunson and Atilla, but Erdogan held out for a guarantee that further prosecutions against Turkey for sanctions violations would end.

A Turkish court ordered that Brunson remain detained, though he was later moved to house arrest, and he has been living since August with his wife, Norine, at his apartment in an old quarter of the seaside city of Izmir. Since then, Turkish courts have several times refused his appeal for release on health grounds.

Washington reacted by imposing financial sanctions on the Turkish interior minister and justice minister. Days later, Trump announced that the United States was doubling its tariffs on steel and aluminum imports from Turkey, just as the Turkish currency, the lira, began a precipitous fall against the dollar.

Erdogan vowed that he would not succumb to threats, and announced retaliatory measures, including increased tariffs on imported US cars, alcohol and leaf tobacco.

The lira, which has lost nearly 40 per cent of its value since the beginning of the year, plunged to a record low, shaking international markets and raising concerns about Ankara’s ability to service its ballooning foreign debt. International credit ratings agencies have repeatedly downgraded Turkey’s standing this year.

The Trump administration seemed unmoved by Turkey’s perilous economic situation, and continued to demand Brunson’s release before it addressed Turkey’s other concerns.