PYONGYANG, North Korea -- After months of anticipation and drama, the release of three American detainees by North Korea played out amid high-stakes diplomacy and was only sealed about an hour before the U.S. secretary of state was wheels-up from Pyongyang. In a statement ahead of their return, the three freed men thanked the U.S. government, President Trump and American people "for bringing us home."

"We thank God, and all our families and friends who prayed for us and for our return. God Bless America, the greatest nation in the world," the three men said in a statement released by the State Department.

The three men, Kim Dong Chul, Kim Hak Song and Tony Kim, walked on their own from a van and onto the plane of Secretary of State Mike Pompeo. It was the culmination of Pompeo's 12-and-a-half-hour visit to the North Korean capital, which included a 90-minute meeting with leader Kim Jong Un.

Get Breaking News Delivered to Your Inbox

Returning to his hotel from that meeting, Pompeo had given reporters a fingers-crossed sign when asked if there was good news about the detainees. A North Korean official came to the hotel shortly after to inform Pompeo that Kim had granted amnesties to the three and that they would be released at 7 p.m. local time, according to a senior U.S. official present for the exchange. Carl Risch, the assistant secretary of state for consular affairs, and a doctor went to another hotel to pick up the men and bring them to the airport, the official said.

They finally left custody at 7:45 p.m. in North Korea, and by 8:42 p.m. they were flying home. Both planes were on the ground in Anchorage, Alaska as 7:25 p.m. ET.

As soon as the plane cleared North Korean airspace, Pompeo called Mr. Trump to inform him of the releases with the men all apparently in good health; then other officials notified their families.

Even before Pompeo's plane had touched down for a stopover at Yokota Air Base in neighboring Japan, the president announced to the world by Twitter that the "3 wonderful gentlemen" were free.

They are the latest in a series of Americans to be held in North Korea for alleged anti-state activities in recent years, only to be freed during the visit of a high-level U.S. official or statesman. Their releases draw a line under an issue that had weighed on plans for a historic summit between Kim and Mr. Trump that will focus on North Korea's nuclear weapons.

"We're granting amnesty to the three detained Americans," the North Korean emissary told Pompeo at Pyongyang's Koryo Hotel, according to the senior U.S. official who requested anonymity to discuss the exchange. "We issued the order to grant immediate amnesty to the detainees."

"That's great," the secretary of state replied.

The North Korean official then advised that the releases would follow a "very brief ceremony" -- which he described as more like a legal process. The official closed with a gentle warning for the U.S. to prevent a repeat: "You should make care that they do not make the same mistakes again," the official said. "This was a hard decision."

North Korea's official news agency, KCNA, said early Thursday that Kim "accepted an official suggestion of the U.S. president for the release of Americans who have been detained in the DPRK for their anti-DPRK hostilities," referring to the initials of the authoritarian nation's official name, the Democratic People's Republic of Korea. The dispatch also made the first official mention by the North of plans for summit between Mr. Trump and Kim.

The Trump administration has made clear it regarded the arrests as politically motivated, and had been bitterly critical of North Korea's refusal to grant consular access to the three, other than a brief visit by a U.S. envoy last June. The envoy had repatriated college student Otto Warmbier, who was sentenced to 15 years in prison with hard labor for stealing a propaganda poster. Warmbier died in June 2017, days after he arrived back in the U.S. with severe brain damage.

Since then, pressure to win the releases of the other three men, whom the administration has referred to as "hostages," has intensified.

Relatively little is known about the men, all Korean-Americans, and the circumstances in which they ended up in North Korean custody.

Kim Dong Chul, a former Virginia resident who had reportedly run a company on North Korea's border with Russia, had been sentenced in April 2016 to 10 years in prison with hard labor after being convicted of espionage. Kim Hak Song and Tony Kim, who had both taught at a university in Pyongyang that was founded with donations from Christian groups, had been held for about one year and apparently had not been tried.

Tony Kim's son Sol Kim, a graduate student in California, is the only relative of the detainees to have appealed in public for their freedom. On learning of the release of Tony, who also uses the name Kim Sang-duk, his family expressed gratitude and credited Mr. Trump for engaging directly with North Korea.

Washington and Pyongyang have remained in a state of war since the fighting in the 1950-53 Korean War ended and do not have formal diplomatic ties, complicating efforts to handle the cases of American detainees. Typically, Swedish diplomats based in North Korea handle U.S. consular affairs.

Pompeo, the most senior U.S. official to meet with a North Korean leader since 2000, was making his second visit to the isolated nation in five weeks -- and less than two weeks after becoming top diplomat. Some in Washington had expressed disappointment that Pompeo's first trip, made in secret when he was still CIA director, did not win the freedom of the three captives.

Pompeo said Wednesday he was "thrilled" the men were now out.

"Though we're not quite back home yet, I welcomed them back," he said, recounting his first words with the men after they were freed. "They were happy to be with us on this plane to be sure," he said.

The three boarded under their own steam, when most of the U.S. delegation had already got on the plane. They were seated with medical personnel in a curtained off area of the aircraft.

After arriving at the stop in Japan, the men transferred to another plane with more medical facilities and were expected to land at Joint Base Andrews outside Washington in the wee hours of Thursday, where Mr. Trump planned to greet them.