SAKRAN, Turkey — A Turkish court on Friday ordered the release of the American pastor Andrew Brunson, ending his 24-month detention and allowing him to fly home, signaling a truce of sorts in a heated diplomatic dispute between Turkey and the United States.
Mr. Brunson, who was accused of spying and aiding terrorists, had been sentenced to 3 years 1 month 15 days in prison. The judge, citing time served, allowed him to leave the country immediately.
Mr. Brunson’s prolonged detention and trial had worsened tensions between the United States and Turkey. President Trump and Vice President Mike Pence personally raised his case several times with President Recep Tayyip Erdogan of Turkey. The United States government imposed financial sanctions. And members of Congress traveled to Turkey to attend his trial.
American officials said Mr. Brunson’s detention, along with 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, Mr. Trump invited him to come to the White House as early as Saturday. “We’re very honored to have him back here with us,” the president told reporters in Ohio, where he was traveling for a campaign rally. “He suffered greatly.”
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.
The decision to free Mr. 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. Turkish officials say they believe Mr. Khashoggi was killed inside the consulate, and Ankara may be trying to mend relations with Washington to secure its help in confronting Saudi Arabia, analysts said.
Mr. 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 Mr. 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, Mr. 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 Rev. 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. American officials had hoped Turkey would also release Serkan Golge, a Turkish-American scientist, and three Turkish citizens who worked at American diplomatic missions.
The Brunson case has been one of several disputes between the two countries.
In particular, Turkey has requested the extradition from the United States of an 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 preacher 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 federal court in Manhattan for his part in the scheme.
In July, the two countries came close to agreeing to a coordinated release of Mr. Brunson and Mr. Atilla, but Mr. Erdogan held out for a guarantee that there would be no further prosecutions against Turkey for sanctions violations.
A Turkish court ordered that Mr. Brunson remain detained, though he was later moved to house arrest. Since August, he had been living with his wife, Norine, at their apartment in an old quarter of the seaside city of Izmir. Turkish courts several times refused his appeal for release on health grounds.
Faced with Turkey’s continuing refusal to free the pastor, Washington imposed 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 rating 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 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.
In a statement on Twitter on Friday, Mr. Pompeo said, “Pastor Brunson is finally coming home to America, following a long ordeal for the pastor and his family. We hope that the Turkish government will quickly release our other detained U.S. citizens and @StateDept locally employed staff.”
The Turkish government had insisted that Mr. Erdogan could not interfere with the judicial process in Turkey. They said Mr. Brunson was charged with serious crimes, including aiding the Kurdistan Workers’ Party, a separatist group that Turkey, the United States and the European Union have designated a terrorist organization.
American officials said the Turkish prosecutors had presented no credible evidence to support their case, and the trial, which unfolded with a hearing every few months, 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, 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 said he had nothing to hide.
The few witnesses for the defense who were allowed to testify described Mr. Brunson as apolitical, focused on his religion, and open to Christians of all ethnic origin, including Syrian refugees.
At his hearing Friday morning, three of the five witnesses who testified contradicted testimony given by one of the state’s main witnesses, seemingly weakening the case against Mr. Brunson.
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 stopped posting statements critical of Turkey on Twitter.
Mr. Erdogan increasingly fell back to explaining that he could not dictate actions to an independent judiciary. Some analysts interpreted that 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 also noticeably toned down their reporting of the case.
The day before Mr. Brunson was released, a small group of Republican and Democratic United States senators issued a statement offering improved ties between the countries as an incentive.
“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 that a deal was near, analysts suggested that Turkey’s economic woes had forced Mr. Erdogan to yield. “The economic pressure is working,” said Asli Aydintasbas, a senior fellow for the European Council on Foreign Relations.
Some pointed to the disappearance of Mr. Khashoggi.
“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.”
Follow Carlotta Gall on Twitter: @carlottagall.
Peter Baker contributed reporting from Washington.