Adm. Noel Gayler welcomes Ernest Brace on his arrival in Tokyo from Hanoi in March 1973. (AP)

Ernest C. Brace, a Marine Corps pilot who was decorated for combat service in Korea, then was court-martialed for desertion after a stateside plane crash and later was celebrated for his fortitude as the longest-held civilian POW in the Vietnam War, died Dec. 5 in Klamath Falls, Ore. He was 83.

The cause was complications from a pulmonary embolism, said his wife, Nancy Brace.

Mr. Brace spent nearly eight years in captivity. Sen. John McCain (R-Ariz.), a former Navy aviator who occupied the cell adjacent to Mr. Brace’s at the infamous prison known as the Hanoi Hilton, once described him as “a man whose bravery and sacrifice for this country have had no bounds.”

By his early 20s, Mr. Brace was a seasoned and accomplished pilot. He flew more than 100 missions in Korea — including one in which he encountered heavy enemy fire and was forced to crash-land in the Sea of Japan, where a U.S. ship rescued him. For those actions, Mr. Brace received the Distinguished Flying Cross.

In January 1961, he was a captain enrolled in an officers’ training course at Quantico, Va., when a plane he was piloting crashed on Maryland’s Eastern Shore. Mr. Brace abandoned the scene, reportedly hitchhiked to Baltimore and turned himself in 10 days later after authorities discovered his discarded flight suit.

He said at the time that he was burdened by domestic and financial problems and had hoped to “get away from it all.” Law enforcement officials alleged that he had attempted to fake his own death in order to allow his family to collect on a life insurance policy.

Mr. Brace insisted that the plane had malfunctioned, and a federal jury acquitted him of purposely destroying the government aircraft. A Marine Corps court martial convicted him of desertion and sentenced him to “punitive dismissal.”

Mr. Brace later became a civilian contract pilot ferrying supplies and passengers into Laos during the Vietnam War. On May 21, 1965, Communist forces attacked his plane on a Laotian airstrip. He was captured and taken to the jungle prisons where he would begin his seven years, 10 months and one week in enemy hands.

That period, he recalled in an account published on the POW Network Web site, would be “spent mostly encaged in small bamboo cages, to be beaten, starved, buried alive, and humiliated as few modern day humans have known.”

Mr. Brace was said to have repeatedly attempted escape. After one effort, the Associated Press wrote, he was buried up to his neck for a week. He recalled that he endured more than four years of solitary confinement. Once, he recalled, he attempted to choke himself.

He eventually was transferred to the Hoa Lo complex in North Vietnam, also known as the Hanoi Hilton, where through taps and furtive messages transmitted between cells, he embarked on a spirit-sustaining friendship with McCain, who had been captured in 1967.

“The wall was like a confessional,” Mr. Brace recalled. “The person on the other side existed in voice only.”

On March 28, 1973, Mr. Brace was released. Soon after, he met McCain at a White House event for former POWs hosted by President Richard M. Nixon.

“A guy came up to me and I looked at him and he said, ‘I’m Ernie Brace,’ ” McCain told an interviewer. “It was such an emotional moment for me.”

In 1974, President Gerald R. Ford granted Mr. Brace a full pardon. Last year, in part through efforts by McCain, he received the Purple Heart and the Prisoner of War Medal.

“As a civilian, Ernie was under no obligation to adhere to the Code of Conduct,” McCain wrote in his memoir “Faith of My Fathers.” “But Ernie’s conscience demanded much from him. He kept our code faithfully. When the Vietnamese offered to release him, he declined, insisting that others captured before him be released first. No one I knew in prison, Army, Navy, Marine, or Air Force officer, had greater loyalty to his country or derived more courage from his sense of honor.”

Ernest Cary Brace was born Aug. 15, 1931, in Detroit. He joined the Marines at 15, his family said, and was commissioned as an officer at the time of his 20th birthday.

He and his first wife, the former Patricia Emmons, had four sons before his service in Vietnam. One son, Patrick, said in an interview this week that his mother remarried during Mr. Brace’s imprisonment because she believed he was dead.

When Mr. Brace returned, his marriage was annulled. In 1974, he married Nancy Rusth, a nurse who had assisted him in his rehabilitation. Besides his wife, survivors include his sons, Ernest Brace of Bonita Springs, Fla., Patrick Brace of Abington, Mass., Michael Brace of St. Petersburg, Fla., and Cary Brace of Rapid City, S.D.; a sister; and eight grandchildren.

After his imprisonment, Mr. Brace told a National Geographic interviewer, he worked in Latin America, China, Russia and the Middle East on programs including narcotics control programs, foreign military sales and fuel contracts.

During the Persian Gulf conflict of the early 1990s, he said that he helped establish a medical evacuation system for firefighters in Kuwait.

Mr. Brace wrote two books, the memoir “A Code to Keep: The True Story of America’s Longest-Held Civilian POW in the Vietnam War” (1988) and “Monkey Paw Soup: And Tales of Drugs, Thugs, Revolution and War” (2012).

Once, Mr. Brace reflected on how he had survived his imprisonment.

“I never thought I would die up there, and escape seemed possible all the time in my head,” he said. “Later, after they had crippled me, I felt that they were keeping me alive for some reason and someday I’d be going home.”

But in the beginning, he observed, “I think my Marine Corps training kept me going.”