The release of a Canadian pastor held in a North Korean prison for more than two years was the result of a long negotiation between Canada and North Korea, CBC News has learned.

Rev. Hyeon Soo Lim, a Presbyterian minister from Mississauga, Ont., arrived in Japan early Thursday, but the story of his release began at the United Nations headquarters in New York, one of the few venues where Canadian and North Korean diplomats meet.

According to a source with knowledge of the negotiations, talks started to get serious around last December, but stalled when North Korea asked that, in return for releasing Lim, Canada name an ambassador or full-time envoy to North Korea.

The Canadian government, however, did not want to have a permanent presence in Pyongyang, preferring a special envoy to conduct diplomacy with a limited purpose for a limited time.

The Kim Jong-un regime allowed two Canadians to visit Lim around Christmas, but the negotiations remained stuck for months.

North Korea complained of Lim's medical costs

During the negotiations with the reclusive regime, the North Koreans raised the issue of Lim's medical bills, which they said ran into the tens of thousands of dollars, the source said. CBC News has not confirmed on the status of those medical bills or whether they were paid before he was released.

The 62-year-old Toronto Presbyterian reverend, who was sentenced to a life of hard labour in prison for crimes against the state, is reportedly in poor health.

He was arrested in 2015 and charged with harming the dignity of the supreme leadership and trying to use religion to destroy the North Korean system, among other things.

​According to media reports earlier this summer, Lim has problems with high blood pressure and complained about pains in the stomach in letters to friends.

Canadian delegation assembled last week

Then last week, the source said, the North Koreans suddenly decided they were open to a visit from a Canadian special envoy.

Daniel Jean, the national security and intelligence adviser to Prime Minister Justin Trudeau, was tasked to head the mission to extricate Lim. Jean was chosen because he isn't a politician and because he was a former deputy foreign affairs minister with extensive knowledge of the file, the source said.

Two directors from Global Affairs Canada, two medical doctors and a member of the Canadian Forces rounded out the six-member delegation.

A Royal Canadian Air Force CC-144 Challenger aircraft lands at the Yokota Air Base in Fussa, Japan carrying Canadian pastor Hyeon Soo Lim and the delegation that secured his release. 0:29

On Saturday, two Royal Canadian Air Force CC-144 Challenger aircraft departed Canada on a two-day flight across the Pacific Ocean.

The twin-engine, long-range executive jets are typically used by Canadian and international VIPs, including the prime minister and the governor general. There are four of these planes operated by the air force, three of which can be used in a medevac role.

"The Challenger has supported many humanitarian and diplomatic efforts," the air force says on its website.

Department of National Defence officials wouldn't elaborate why half of its Challenger fleet was dispatched for this mission, but the source with knowledge of the negotiations said it was for "precaution."

Video from TV Asahi shows Rev. Hyeon Soo Lim arriving on a Royal Canadian Air Force aircraft in Yokota Air Base, Fussa, Japan, on Thurs., Aug. 10, after North Korea released the imprisoned pastor. 0:28

The two jets landed at the Yokota Airbase, a U.S. military air force base in Fussa, Japan, just outside of Tokyo, on Tuesday afternoon; one then continued on a two-hour flight to Pyongyang with the delegation aboard.

Within minutes of touching down in the internationally isolated capital, KCNA — North Korea's official news agency — announced that the delegation arrived.

The plane dropped off Jean's team, and flew back to Japan to await further developments. Canadian officials did not want the Challenger to remain on the Pyongyang tarmac because of "security concerns" and were afraid the plane could be "vulnerable," the source said.

Delegation spent 40 hours in Pyongyang

Flight records analyzed by CBC News indicate the Canadian delegation spent about 40 hours in the North Korean capital from Tuesday evening until Thursday morning.

State-run media reported Lim was freed on "sick bail" about 25 hours after the Canadian government delegation arrived to discuss the case.

It is still unknown who Canada's national security adviser met with and what was discussed to secure Lim's release.

But the source with knowledge of the negotiations insists: "Canada did not concede anything."

Lim and the Canadian delegation arrive at Yokota Air Base on a Royal Canadian Air Force plane Thursday after his release. (Toru Nagasawa)

North Korea used the Lim case "for domestic purposes," the source added, pointing out that they were the ones that announced the envoy had arrived and that Lim had been released from prison in state media.

It would be another 15 hours after Lim was freed by North Korean court before a Canadian Forces plane returned to Pyongyang to collect the delegation and Lim.

They touched down at the Yokota Airbase in Japan, shortly after 1 p.m. Thursday local time.

After keeping tight-lipped about Lim's release, Trudeau confirmed he was free in a statement released only after the pastor left North Korean airspace and landed in Japan.

"The government of Canada was actively engaged on Mr. Lim's case at all levels," Trudeau said.

"Pastor Lim's health and well-being remain of utmost importance to the government of Canada, and we are working to ensure that he receives any required medical attention," he continued.

The pastor was seen in footage broadcast by Japan's TV Asahi, walking, shaking hands and speaking with members of the delegation that travelled to North Korea to secure his release. Lim appeared thinner but in good spirits.

A family spokesperson said Lim is expected back in Toronto Friday at the earliest, and possibly not until Saturday.

She said the family isn't hoping for a quiet homecoming and hopes to see his parishioners and the media at the airport to greet him when he lands.