A Turkish court on Friday convicted an American pastor of terror links but released him from house arrest and allowed him to leave the country.

His departure removed a major irritant in fraught ties between two Nato allies that still disagree on a host of other issues.

The court near the western city of Izmir sentenced Andrew Brunson, 50, to just over three years in prison for allegedly helping terror groups, but let him go because the evangelical pastor had already spent nearly two years in detention.

Turkey American Pastor
A car with Andrew Brunson and US charge d’affaires Jeffrey Hovenier sitting inside heads to the airport after his release following his trial (Emrah Gurel/AP)

An earlier charge of espionage was dropped.

Hours later, Brunson was transported to Izmir’s airport and was flown out of Turkey, where he had lived for more than two decades.

“I love Jesus. I love Turkey,” an emotional Brunson, who had maintained he was innocent of all charges, told the court during Friday’s hearing.

He tearfully hugged his wife Norine Lyn as he awaited the court decision.

“Pastor Brunson just released will be home soon!” US President Donald Trump tweeted after the American was driven out of a Turkish prison in a convoy.

The release of Brunson was a diplomatic triumph for Trump, who is counting on the support of evangelical Christians for Republican candidates ahead of congressional elections in November.

It could also benefit Turkey, allowing the government to focus on an escalating diplomatic crisis over Jamal Khashoggi, a Saudi contributor to The Washington Post who went missing more than a week ago and is feared dead after entering the Saudi consulate in Istanbul.

Additionally, Turkey could now hope that the US will lift tariffs on Turkish steel and aluminium imports, injecting some confidence into an economy rattled by high inflation and a mountain of foreign currency debt.

Friday’s ruling followed witness testimony that seemed to partly undermine the prosecutor’s allegations and highlighted concerns that Turkey had been using the US citizen as diplomatic leverage.

The court dropped an espionage charge against Brunson, who had faced up to 35 years in jail if convicted of all the charges against him.

He was among tens of thousands of people, mostly Turks, who were caught up in a government crackdown after the failed coup.

Earlier, the court called two witnesses following tips from witness Levent Kalkan, who at the previous hearing had accused Brunson of aiding terror groups.

The new witnesses did not confirm Kalkan’s accusations. Another witness for the prosecution said she did not know Brunson.

Brunson again denied accusations that his church aided Kurdish militants, saying he had handed over a list of Syrian refugees whom the congregation had helped and adding that Turkish authorities would have identified any terrorists.

“We helped everyone, Kurds, Arabs, without showing any discrimination,” he said.

The pastor, who is originally from Black Mountain, North Carolina, was imprisoned for nearly two years after being detained in October 2016.

He was formally arrested in December of that year and placed under house arrest on July 25 for health reasons.

Turkish President Recep Tayyip Erdogan had resisted US demands for Brunson’s release, insisting that the courts are independent.

Brunson led a small congregation in the Izmir Resurrection Church.

While supporters in the United States celebrated Brunson’s release, his case overshadowed the predicament of a Turkish-American scientist from Nasa and several Turkish workers for the US diplomatic mission who were arrested in Turkey.