By Carlotta Gall, New York Times

Sakran, Turkey: A Turkish court on Friday ordered the release of American pastor Andrew Brunson, ending his 24-month detention and allowing him to fly home, signalling a truce of sorts in a heated diplomatic dispute between Turkey and the United States.

Brunson, who was accused of spying and aiding terrorists, had been sentenced to three years, one month and 15 days in prison. The judge, citing time served, allowed him to leave the country immediately.

Brunson’s prolonged detention and trial had worsened tensions between the United States and Turkey. US President Donald Trump and Vice-President Mike Pence personally raised his case several times with President Recep Tayyip Erdogan of Turkey. The US government imposed financial sanctions. And members of Congress travelled to Turkey to attend his trial.

US officials said Brunson’s detention, and that of about 20 other Turkish-Americans held after a coup attempt in 2016, was an attempt by Ankara to gain leverage in its various disputes with Washington.

After the pastor’s release, Trump invited him to come to the White House as early as Saturday. “We’re very honoured to have him back here with us,” he told reporters.

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.

The decision to free Brunson came a little over a week after the disappearance of Jamal Khashoggi, a Saudi dissident and columnist for The Washington Post, at the Saudi Consulate in Istanbul. Ankara may be trying to mend relations with Washington to secure its help in confronting Saudi Arabia, analysts said.

Trump, however, seemed to want to put distance between the Brunson and the Khashoggi disputes.

“We went through a system and we got him out,” he said of Brunson. “We tried to get him out for a long time. This has nothing to do with anything, and there’s no deal there at all.”

After the hearing, Brunson, an evangelical pastor who runs the small Resurrection Church in Izmir, left the courthouse by car and returned to his home before departing for Germany, en route to the United States.

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

Washington and Ankara had been involved in complex negotiations over the pastor’s fate for months. US officials had hoped Turkey would also release Serkan Golge, a Turkish-American scientist, and three Turkish citizens who worked at US diplomatic missions.