During the first harrowing months in the desert, he and the others were blindfolded, bound and moved countless times. The threat of execution hung over them. In one of the many ransom videos they were forced to make, they had to wear orange “Guantánamo” suits, like those worn by the detainees at the United States military prison in Cuba, and to plead for their lives.
“I tried to explain that I am Swedish,” he recalled. “That we also think Guantánamo is unlawful and counterproductive.”
When he tried to get a sense of his kidnappers’ motives, they made only vague reference to the invasion of Iraq, the war in Afghanistan and satirical cartoons of the Prophet Muhammad that set off protests when they were published by a Danish newspaper in 2005.
Four months into captivity, the hostages made a strategic decision to convert to Islam. “It was to save my life,” he said. After the conversion they were no longer isolated, shackled or forced to plead for their lives in the many ransom videos that were made.
“I see that as the most clear evidence that it actually helped change my situation,” he said.
Mr. Gustafsson told his captors that his government would never pay. When he was released, Sweden’s foreign minister said it was the result of years of diplomatic efforts, not ransom.
But Magnus Ranstorp, a counterterrorism and security expert with the Swedish Defense University, said it was unlikely that some form of exchange did not take place.
“The only thing we know for sure is that not a single hostage has been released without payment,” he said. “It’s not a charitable organization.”
A retired European intelligence official, who spoke on the condition of anonymity, said that 3.5 million euros, or about $4.2 million, had been paid and negotiated through the South African charity Gift of the Givers Foundation for the release of Mr. McGown.
While the United States and Britain adhere to strict no-ransom policies, countries like France and Germany have taken bags full of cash disguised as humanitarian aid to the desert.
From his view inside their camps, Mr. Gustafsson said it was evident his captors had resources.
“They’re well financed nowadays,” he said. “They say they didn’t used to be, but now they are, and it’s not difficult to figure out that that is actually the money that has been paid by European governments.”
A 2014 New York Times tally of ransoms collected by Al Qaeda’s affiliates found that the group had taken in at least $125 million since 2008.
Al Qaeda in the Islamic Maghreb rose to prominence more than a decade ago largely because of extraordinary ransom payments, which started in 2003 with the abduction of 32 European tourists who were freed after governments paid an estimated €5 million, or about $6 million.
After their conversion to Islam, the hostages prayed, ate and sat with their kidnappers. There was no need for prison walls when the Sahara stretched for hundreds of miles in every direction.
For the next five years, they moved hundreds of times, living outdoors and being guarded by about a dozen rotating men and boys.
Mr. Gustafsson said life in the desert was a cross between “a prison sentence and Robinson Crusoe.”
“We are a group of young guys hanging out in a sandbox, living through the same things — sand storms, problems with the car. If we don’t have water, it’s the same for all of us.”
Between prayers, he occupied himself with exercising and learning the languages of his captors — local languages, Arabic, French.
Mali, a former French colony, has seen decades of tension between the south, where the economic and political power is concentrated, and the minority populations in the north. In the beginning of 2012, a Tuareg separatist movement calling for a new state, “Azawad,” swept down from the north.
In the same year, the military in Mali, displeased with the government’s handling of the rebellion, ousted the president. The north came under rebel control while Al Qaeda in the Islamic Maghreb capitalized on the subsequent power vacuum and took over Timbuktu for a time.
In January 2013, French forces moved into the north. There were fighter planes in the air, and the kidnappers were constantly on the run, decamping at the first hint of a surveillance plane or anything suspicious, said Mr. Gustafsson. The hostages stopped counting the moves after they got to 100.
Mr. Gustafsson saw the flights above as an opportunity to escape, and he tried it once, walking into the desert, thinking that they might not dare follow him.
But after he wandered in the desert for two nights, his captors tracked him down. “I think actually I would have walked to my death,” he said.
His kidnappers were a motley crew with varying levels of piety. The leaders belonged to a tough, power-hungry religious sect based in Algeria. They had quietly infiltrated Mali over the past 15 years, trying to impose Shariah law and recruiting radicalized migrants from nearby countries who were in it for the cause — and the adventure.
The local recruits from Mali, mostly Arabs and Tuaregs from the desert and desert cities, were motivated by financial opportunity, according to Mr. Gustafsson, as well as a hatred toward the power wielded in the south. They were illiterate, but knew everything about surviving in the desert.
Although Mr. Gustafsson was freed in June, it was only recently that he stopped saying “In the name of God” before every meal. Other habits acquired in the desert will take longer to change. He still sleeps with his head under the covers to “keep out the sand,” and he doesn’t bury his hands under the pillow where scorpions might burrow.
“Of course it changes you,” he said. “In the end, it’s difficult to put your finger on. But I’m getting out of it. I’m learning to switch.”
Will he ever go back to the Sahara? He said he would, if it were safe.
“I’m not going to miss those guys, but I’m going to miss the desert, the vastness, the night skies,” he said. “When you live there, you learn the landscape. You know where the wind comes from at different times of the year. You know how the storms move. All of this is just so majestic.”