Sgt. Bowe Bergdahl, the last known American POW, was freed after five years in captivity — an ordeal that began and ended in Afghanistan under a shroud of mystery.

The Taliban turned over Bergdahl Saturday morning to US special forces in exchange for five notorious Islamic militants who had been held at Guantanamo Bay and will be sent to Qatar, where they will stay for a year under the terms of the trade.

At least one of the prisoners, ranking Taliban leader Khairullah Khairkhwa, had direct ties to Osama bin Laden.

Bergdahl was picked up by helicopter in western Afghanistan, near the Pakistan border.

After climbing aboard, the 28-year-old Idahoan, trying to communicate with his rescuers over the roar of the rotors, scrawled “SF?” on a paper plate — asking his rescuers whether they were special forces.

“Yes,” one of the men shouted. “We’ve been looking for you for a long time.”

The Army infantryman — himself nicknamed “SF” by his comrades for his gung-ho interest in special-forces tactics — began to weep.

Bergdahl’s parents, who had lobbied continuously for his ­release, had not seen him by Saturday night, but intimated that he faces an arduous recovery from his ordeal.

Bergdahl is speaking in what appears to be Pashto, said his dad, Bob Bergdahl. It was not clear whether his son can still even speak English, Bob said.

When the father spoke to his son — for the first time in five worried years — it was to say both in Pashto and English, “I am your father, Bowe.”

“We will continue to stay strong for Bowe while he recovers,” said his mom, Jani.

The search for Bergdahl began soon after he went missing on June 30, 2009, in the same rugged wilds of southeastern Afghanistan where NFL player-turned-Army Ranger Pat Tillman was killed.

Bergdahl’s mysterious disappearance from the small military outpost there and the subsequent revelation that he was in enemy hands prompted questions that still linger.

When the father spoke to his son — for the first time in five worried years — it was to say both in Pashto and English, ‘I am your father, Bowe.’

Soon after the capture, Taliban commander Mulvi Sangeen claimed a drunken Bergdahl was snatched while he stumbled to his car in the Yousaf Khel district of Paktika.

The US military called that a lie, and in one of the videos taken during his captivity, Bergdahl himself said he was captured while lagging behind a patrol.

But in the weeks before his capture, Bergdahl had made murky statements that suggested he was gravitating away from the soldiers in his unit and toward ­desertion, a member of his platoon told Rolling Stone.

“He spent more time with the Afghans than he did with his platoon,” former Spc. Jason Fry told the magazine in 2012.

As a teen, the home-schooled son of Calvinists took up ballet — recruited to be a “lifter” by “a beautiful local girl,” Rolling Stone reported, “the guy who holds the girl aloft in a ballet sequence.” The strategy worked: Bergdahl — who also began dabbling in Budd­hism and tarot card reading — soon moved in with the woman.

Even as a teen, he could fire a .22-caliber rifle with precision.

At age 20, he traveled to Paris and started learning French in hopes of joining the French Foreign Legion.

His application was rejected, and he was devastated, the magazine reported.

Bergdahl would drift for years, working mainly at a coffee shop near home. He briefly considered moving to Uganda to help villagers being terrorized by militias before deciding on a different ­adventure.

“I’m thinking of joining the Army,” he told his folks after ­already having signed up.

Bergdahl’s dream was to help Afghan villagers rebuild their lives and learn to defend themselves, his dad told the magazine.

“The whole ‘COIN’ thing,” Bob explained, referring to America’s strategy of counter-insurgency. “We were given a fictitious picture, an artificially created picture of what we were doing in ­Afghanistan,” the dad said.

Bowe Bergdahl would detail his disillusionment with the Afghanistan campaign in an email to his parents three days before he went missing.

“I am sorry for everything here,” he wrote. “These people need help, yet what they get is the most conceited country in the world telling them that they are nothing and that they are stupid.”

Bergdahl also complained about fellow soldiers. The battalion commander was a “conceited old fool,” he said, and the only “decent” sergeants, planning to leave the platoon “as soon as they can,” told the privates — Bergdahl then among them — “to do the same.”

“I am ashamed to be an American. And the title of US soldier is just the lie of fools,” he concluded. “I am sorry for everything. The horror that is America is disgusting.”

Bob Bergdahl responded in an email: “OBEY YOUR CONSCIENCE!”

One night, after finishing a guard-duty shift, Bowe Bergdahl asked his team leader whether there would be a problem if he left camp with his rifle and night-vision goggles — to which the team leader replied “yes.”

Bergdahl then returned to his bunker, picked up a knife, water, his diary and a camera, and left camp, according to Rolling Stone.

The next morning, he was reported missing, and later that day, a drone and four fighter jets ­began to search for him.

Weeks of searching turned into months. The military pushed his parents and fellow soldiers to sign nondisclosure agreements. But before everyone signed, a comrade from his unit publicly called on Facebook for Bergdahl’s execution as a deserter.

Propaganda videos of his captivity — which featured Bergdahl denouncing American foreign policy — were released.

At least once, in 2011, the prisoner, looking more haggard, fought back and tried to escape.

“He fought like a boxer,” a Taliban fighter told Newsweek.

Why Bergdahl was captured in the first place remained a mystery by the time high-level US government talks began in 2012 regarding a trade for his release.

“Frankly, we don’t give a s–t why he left,” one White House official said at the time. “He’s an American soldier. We want to bring him home.”

There was fierce debate over exchanging him for the five Taliban combatants. Sen. John McCain, himself a former POW, once described the five as “the five biggest murderers in world history,” according to Rolling Stone.