A Turkish court on Friday ordered the release from house arrest of an American pastor at the heart of a diplomatic dispute between Turkey and the U.S., a day after NBC News reported the two countries had struck a deal to allow him to walk free.

The court sentenced Andrew Brunson to three years and one-and-a-half months in prison after convicting him on terrorism charges. But he was ordered released on time served and allowed to leave the country after spending nearly two years in detention.

The North Carolina native broke down in tears when the decision was announced, witnesses said. He had earlier made an impassioned plea to the judge: "I am an innocent man. I love Jesus. I love Turkey."

Andrew Brunson, center, sits inside a car as he arrives for his trial in Izmir Friday. DHA / AP

Brunson, 50, was one of two dozen Americans charged with helping Kurdish militants and supporters of Fethullah Gulen, the cleric whom Turkey blames for plotting a failed coup attempt in 2016.

The Trump administration immediately demanded his release, triggering a diplomatic standoff.

Brunson was facing a possible sentence of up to 35 years in prison if convicted on all charges, which included espionage.

News of his release was celebrated by politicians and religious leaders across the U.S.

President Trump tweeted, "Pastor Brunson just released. Will be home soon."

Reached by phone, Brunson's daughter Jacqueline Furnari, 20, told NBC News she was elated by the news.

"I am still in shock," she said from Texas. "It’s just amazing after two years of going through this ordeal, he is finally coming home."

Following the court hearing, Brunson was seen returning to his home in the Turkish coastal province of Izmir, according to Reuters. He was expected to depart for the U.S. in the coming hours.

There was some initial confusion over Brunson's travel plans after the American Center for Law and Justice, a conservative group that has been working on the pastor's release, said he was already on a plane.

On Thursday, NBC News learned that the White House expected Brunson to be released by the Turkish government and returned to the U.S. in the coming days.

Senior administration officials said Brunson was freed as part of a deal the Trump administration cut with the Turkish government in the past couple of weeks. Under the agreement the U.S. is to lift sanctions Trump imposed on Turkey in August after Turkey refused to release Brunson despite repeated talks between U.S. and Turkish officials.

The breakthrough came as Turkey is seeking support from the U.S. in the case of Jamal Khashoggi, the Washington Post columnist who vanished after visiting the Saudi Arabian consulate in Istanbul earlier this month. The Turkish government is "100 percent confident" that Khashoggi, a Saudi citizen and outspoken critic of his country's rulers, was killed inside the consulate, a Turkish official told NBC News.

Trump denied there was a deal made for Brunson's release upon his arrival in Ohio for a Friday evening rally. The president told reporters that Brunson's return "has nothing to do with anything" and that the government has been trying to get him released for "a long time."

"But we're happy to have him and have him in good shape. I hear he's in very good shape," Trump said.

The White House released a statement on Friday commending the "strength shown by Pastor Brunson and his family over a difficult two years." The White House statement added that it remained "deeply concerned" about the detention of other U.S citizens in Turkey.

Evangelist Franklin Graham, son of the late pastor Billy Graham, had spent years using his considerable platform to advocate for Brunson's release. Graham told NBC Newsthat he had spoken personally to Trump about Brunson's situation while he was being detained and that he "didn't even have to put it on their radar."

"And several times I have been at the White House, the president has mentioned 'we are gonna get this man back,'" Graham said. "And so I am just grateful we did what we could to try and make publicity for it, to make people aware."

Earlier Friday, heavy security greeted Brunson as he arrived to the courthouse in a convoy of vehicles before daybreak. Brunson appeared in the courtroom wearing a black suit, white shirt and red tie. His wife, Norine, looked on from the visitors' seating area as he listened to testimony from defense and prosecution witnesses.

"I do not understand how this is related to me," Brunson said after the judge questioned one of a series of witnesses heard before a lunch recess. He said the judge was asking the witness about incidents Brunson was not involved in.

Witnesses also told the court that previous testimony attributed to them against the pastor was inaccurate.

One denied telling prosecution witness Levent Kalkan that a member of Brunson's church was linked to militants.

"I did not say it to Mr. Kalkan. I heard it from him," the witness said.

"I am really shocked now," Kalkan responded. Kalkan also told the court that some of his previous testimony had been "misunderstood.”

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

Yuliya Talmazan reported from London; Aziz Akyavaz from Izmir.