ALIAGA, Turkey -- A Turkish court freed American pastor Andrew Brunson on Friday after he spent nearly two years in jail and more time under house arrest on charges related to terrorism and espionage. Brunson had become entangled in a diplomatic dispute between Turkey and the United States and American officials had been pushing hard for his release.

His lawyer, Ismail Cem Halavurt, told Reuters he will spend two days in Germany before he heads to the U.S.

The court in the city of Aliaga found Brunson guilty of terror charges and sentenced him to three years and one month in prison, but released him on time served. That allowed him to leave the country.

Get Breaking News Delivered to Your Inbox

Turkey's government had faced threats of further U.S. sanctions over its treatment of the pastor, who denies all charges against him, but President Recep Tayyip Erdogan always insisted that the Turkish judicial system be allowed to function.

A lawyer for Brunson said Friday that he was on his way back to the U.S.

My thoughts and prayers are with Pastor Brunson, and we hope to have him safely back home soon! — Donald J. Trump (@realDonaldTrump) October 12, 2018

"We're grateful to the president, members of Congress and diplomatic leaders who continued to put pressure on Turkey to secure the freedom of Pastor Brunson," lawyer Jay Sekulow, who represents Brunson's family, said in a statement received by the Reuters news agency. "The fact that he is now on a plane to the United States can only be viewed as a significant victory for Pastor Brunson and his family."

Brunson told the court on Friday that he is "an innocent man. I love Jesus, I love Turkey."

Friday's hearing took place in a prison complex near the western city of Izmir. Brunson arrived in a secured convoy before daybreak. The evangelical pastor was convicted of terror-related charges, but he was also accused of espionage. Prosecutors had been seeking up to a 35-year sentence.

Brunson, 50, who has lived in Turkey for more than two decades, has always rejected the charges and strongly maintained his innocence. He is one of thousands caught up in a wide-scale government crackdown that followed a failed coup against the Turkish government in July 2016.

Prosecutors accused Brunson of committing crimes on behalf of terror groups, linking him to outlawed Kurdish militants and a network led by a U.S.-based Turkish cleric who is accused of orchestrating the coup attempt. The U.S. maintained that he was being held unjustly and repeatedly called for his release.

State Department spokeswoman Heather Nauert told reporters on Thursday that the U.S. was hopeful he would soon go free but said she was unaware of any agreement on his release.

The pastor, who is originally from Black Mountain, North Carolina, was imprisoned for nearly two years -- detained in October 2016 and formally arrested in December that year -- before being placed under house arrest on July 25 for health reasons.

The court's decision failed to improve tensions between the two NATO allies. Washington slapped sanctions on two Turkish officials and doubled tariffs on Turkish steel and aluminum imports. Those moves in August, coupled with concerns over the government's economic management, helped trigger a Turkish currency crisis.

Turkish President Recep Tayyip Erdogan had resisted U.S. demands for Brunson's release, insisting that the courts are independent. But he had previously suggested a possible swap of Brunson and the Pennsylvania-resident Fethullah Gulen -- the cleric accused of being behind the coup.

Brunson led a small congregation in the Izmir Resurrection Church. The U.S. Commission on International Religious Freedom, with representatives monitoring the trial, has listed him as a "prisoner of conscience."

William Devlin, an evangelical pastor from New York spoke to reporters outside the prison, saying hundreds of thousands of Christians are praying for Brunson's release.

Brunson's lawyer took the case to Turkey's highest court last week seeking his release from house arrest.