SAKRAN, Turkey — A Turkish court on Friday ordered the release of the American pastor Andrew Brunson from house arrest, a move that will end his 24-month imprisonment and allow him to fly home, and that signaled a truce of sorts in a heated diplomatic dispute between Turkey and the United States.
Mr. 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 behavior and in view of time served.
He left the courthouse by car shortly after the decision was announced, and American 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 Mr. 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.
Mr. Brunson’s prolonged detention and trial significantly raised tensions between the United States and Turkey, with President 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 ha 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 Mr. 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 United States Treasury is expected to impose on the state-owned Turkish bank, Halkbank, for its part in a conspiracy to violate American sanctions against Iran.
Mr. 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.
Turkish officials say they have video and audio evidence that Mr. Khashoggi, a United States resident, was killed, and his case may have compelled Turkey to seek to repair relations with Washington to secure its help in confronting Saudi Arabia, analysts said.
Washington has accused Ankara of holding Mr. Brunson, along with roughly 20 Turkish-Americans and three Turkish employees of the American 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 the Islamist preacher Fethullah Gulen, whom it accuses of running a terrorist organization and of instigating the 2016 coup attempt. Mr. Erdogan once suggested a swap of the cleric and the pastor.
Mr. 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 Mr. Brunson and Mr. Atilla, but Mr. Erdogan held out for a guarantee that further prosecutions against Turkey for sanctions violations would end.
A Turkish court ordered that Mr. 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, Mr. 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.
Mr. Erdogan vowed that he would not succumb to threats, and announced retaliatory measures, including increased tariffs on imported American cars, alcohol and leaf tobacco.
The lira, which has lost nearly 40 percent 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 Mr. Brunson’s release before it addressed Turkey’s other concerns.
In a Twitter post in August, Mr. Trump referred to Mr. Brunson as a “great patriot hostage.”
“We will pay nothing for the release of an innocent man, but we are cutting back on Turkey!” Mr. Trump declared.
The Treasury secretary, Steven Mnuchin, also warned in comments to reporters that the government would take further actions if Mr. Brunson was not released quickly.
Mr. Brunson has had high-level support from the Trump administration, not least because he and Secretary of State Mike Pompeo belong to the same denomination of the Evangelical Presbyterian Church in the United States, according to Mr. Devlin, the New York pastor.
The Turkish government has insisted that Mr. Erdogan cannot interfere with the judicial process in Turkey, and officials have emphasized that Mr. Brunson is charged with serious crimes, including espionage and aiding the Kurdistan Workers’ Party, the P.K.K., a separatist group that Turkey, the United States and the European Union have designated a terrorist organization.
American officials have said that the Turkish prosecutors presented no credible evidence to support their case, and the trial, which has unfolded with a hearing every few months, has produced few hard facts to support the notion that Mr. Brunson was involved in terrorism.
Witnesses, mostly police informants or former members of his church, have accused him of voicing support for Kurdish separatists, and serving as a link to supply weapons and support to Kurdish rebels in Syria. Mr. Brunson, who faces up to 35 years imprisonment, denied all the claims.
Three of the five witnesses who appeared in court Friday morning — two of them by video link — contradicted testimony given by one of the state’s main witnesses, seemingly weakening the case against Mr. Brunson.
The few witnesses for the defense who have been allowed to testify have described Mr. Brunson as apolitical, focused on his religion, and open to Christians of all ethnic origin, including Syrian refugees.
Mr. Brunson said he had nothing to hide and described how the windows of his church, which is housed in a small, single-room house in Izmir, were always open for anyone to listen in.
There had been signs that the two sides were working to resolve the dispute. The political rhetoric was toned down, and after his outbursts against Turkey in August, Mr. Trump ceased posting tweets critical of Turkey.
Mr. Erdogan increasingly fell back to explaining that he could not dictate actions to an independent judiciary, which was interpreted by some analysts as a sign that he was preparing to pass off Mr. Brunson’s release as the decision of the court.
“I am not in a position to intervene with the judiciary, since Turkey is a constitutional state,” Hurriyet Daily News quoted him as saying on Thursday. “I must obey whatever decision the judiciary gives. All related parties must follow the judicial rulings. Period.”
Turkish newspapers, which have accused Mr. Brunson of being a spy and of having links to the “Fethullah Gulen terrorist organization,” have noticeably toned down their reporting of the case.
Articles on Friday morning noted only briefly that Mr. Brunson would appear before the court for the fourth hearing of his case.
Turkish officials have inferred that the Trump administration wants to welcome Mr. Brunson home as a way to lift the Republican Party before midterm elections. Officials from the East-West Institute, a think tank with offices in New York and Istanbul, were among those present at the trial, and said they had been helping repair Turkish-American relations and expected Mr. Brunson to be freed on Friday.
A small, bipartisan group of United States senators offered further improvement in bilateral ties if Brunson were freed in a statement released on Thursday.
“It is our greatest hope that the pastor will finally be allowed to return home to his family in the United States after his hearing tomorrow,” they wrote. “The United States and Turkey are NATO allies and have a number of mutual concerns regarding regional security and stability. It is time that we close this ugly chapter in our relations.”
As speculation rose before Friday’s court hearing that a deal would hold, analysts suggested that Turkey’s economic woes had forced Mr. Erdogan to yield in Mr. Brunson’s case. “The economic pressure is working,” said Asli Aydintasbas, a senior fellow for the European Council on Foreign Relations.
The Khashoggi case may have offered a chance for the two sides to cooperate. “It opens a window for Erdogan,” said Soner Cagaptay, director of the Turkish Research Program at the Washington Institute for Near East Policy. “If he does something favorable, if Brunson can board a plane, the U.S. would help Erdogan over Khashoggi.”