Andrew Brunson, the American pastor who was imprisoned and then placed under house arrest in Turkey due to his alleged ties to an outlawed group, was ordered freed on Friday and sentenced to time served -- and is now expected to stop in Germany for a medical exam before returning to the U.S.

The Turkish judge's ruling ended a tense diplomatic standoff between the U.S. and Turkey that began after Brunson’s October 2016 detention on terror and treason charges. Turkey targeted the 50-year-old pastor as part of a massive government crackdown following a failed coup months earlier. Brunson was officially sentenced to three years and one month in prison for the conviction; but, because he had already served two years in detention, he wasn't required to spend any more time behind bars.

“This is the day our family has been praying for – I am delighted to be on my way home to the United States," Brunson said in a statement provided by the American Center for Law and Justice, which had been working to secure his freedom.

Brunson appeared Friday at a prison complex in Izmir for his fourth hearing, telling the courtroom he's "an innocent man."

“I love Jesus. I love Turkey,” Brunson said, according to the Associated Press. Witnesses later said he wept as the verdict was announced.

His lawyer, Ismail Cem Halavurt, said Brunson was expected to leave Turkey for the United States later today. Sources told Fox News that the pastor would stop in Germany for a medical exam before flying over the Atlantic. He is expected to arrive at Andrews Air Force Base in Maryland early Saturday morning.

Prosecutors had accused Brunson of acting on behalf of terror groups, specifically linking him to outlawed Kurdish militants and a network led by U.S.-based Turkish cleric Fethullah Gulen. Gulen is accused of orchestrating the 2016 coup attempt.

Brunson -- who is from North Carolina and led a small congregation in the Izmir Resurrection Church -- had routinely denied the accusations made against him. He appeared at the hearing Friday wearing a black suit, white shirt and red tie, while his wife, Norine, watched from the visitors seating area, according to Reuters.

The court on Friday called two witnesses, following alleged tips from witness Levent Kalkan, who at a previous hearing had accused Brunson of aiding terror groups. But the new witnesses didn't confirm Kalkan's story and another prosecution witness said she did not even know Brunson.

Brunson on Friday also again denied accusations his church aided Kurdish militants. Brunson said he handed over to authorities a list of Syrian refugees the congregation had helped and believed Turkish officials would then have identified any terrorists.

"We helped everyone, Kurds, Arabs, without showing any discrimination," Brunson said, according to the AP.

Brunson was arrested and detained for 18 months before finally being charged by the Turkish government with a connection to the coup attempt.

U.S. officials also refuted the accusations against Brunson, and amid mounting pressure from American politicians, Brunson was moved from prison to house arrest in July.

Sen. Ben Sasse, R-Neb., hailed the verdict Friday, calling it "long overdue news" and adding that "the Turkish government should put Pastor Brunson on the next flight home."

The Trump administration advocated persistently for Brunson's release, leading to an intense economic showdown between the two NATO allies. In August, the U.S. slapped sanctions on an array of Turkish officials and on some goods, sending Turkish currency into freefall.

Brunson was not the only American behind bars in Turkey on questionable charges. Several Turkish-American citizens -- including NASA scientist Serkan Golge and several workers from the United States Embassy in Ankara -- are languishing in prison alleged to have a connection to Gulen's group.

Fox News’ Hollie McKay, John Roberts, Kathleen Joyce and the Associated Press contributed to this report.