One afternoon two years into my captivity, in a dirty villa, I sat up on the mattress and noticed that my guard had left the room. His rifle lay on a mat. I considered grabbing it. The pirates were holding me in Galkacyo, a regional capital in central Somalia. They told me it was Haradheere, near the coast, but I knew Haradheere had no commercial airport, and at first the pirates would giggle every time we heard passenger planes take off and land.

I had seen the dull and dusty buildings of Galkacyo’s airport as a free man, and now the aircraft noise inspired baroque dreams of freedom – fantasies ranging from a quiet release on the tarmac to a clandestine gathering of Black Hawks and commandos in the dead of night.

My guard, Bashko, came in and noticed the gun. He picked it up by the muzzle, nimbly, and sat down with a brilliant smile.

“Problem!” he said, meaning the unattended firearm.

He rested it behind him and munched a stem of khat, a leafy green plant that acts as a stimulant. His eyes were fervid. I had just been wondering how many of the guards I could shoot before they shot me. I smiled. I was – or had been – a peaceful man. I didn’t want to kill him, or anyone. But I was going nuts.

“Michael,” Bashko said with good humour. “If the Americans come, you will be killed.”

“I know.”

“Why no money?” he asked, referring to the ransom the pirates had demanded.

I shrugged.

I flew to Somalia in early 2012 to write about a pirate gang jailed in Hamburg. They had been captured two years earlier when they tried to hijack the MV Taipan, a German cargo ship, near Somalia. Their marathon trial represented the first proceeding on German soil against any pirate, Somali or otherwise, in more than four centuries. I had reported on the case for Spiegel Online, where I worked in Berlin, and it seemed to me that a book about the case and some underreported aspects of Somali piracy might be interesting.

I travelled with Ashwin Raman, a Indian-born film-maker, whose documentaries about Afghanistan and Somalia had won several awards. We had arranged security through Mohammed Sahal Gerlach, a Somali elder, in Berlin. Gerlach had lived much of his adult life in Germany, but he came from Galkacyo, which had become a latter-day pirate supply town. Gerlach had good relationships with the dominant Sa’ad clan elders in the region. He had also guided a German TV correspondent through the same region about eight months before.

During the trial in Hamburg, some of the public defenders had insisted their clients were poor, simple, press-ganged fishermen. The notion of Somali pirates as frustrated fishermen was a cliche, but it seemed to work in court, where little could be verified about the men. When we arrived in Somalia, we found this fishing story in common circulation. We heard it in Hobyo, a pirate nest on the eastern coast where we travelled with a long convoy of guards. As guests of Gerlach and his Sa’ad clan, we interviewed a boss who called himself Mustaf Mohammed Sheikh. He preferred to keep his face wrapped in a keffiyeh and declared himself to be at war with forces of the west. He said “white people” had attacked Somalia by trawling its coral reefs and dumping poison on its shores. Some complaints were legitimate – overfishing and illegal dumping are enormous problems along the African coast – but pirates throughout history have piggybacked on romantic social causes, and Somalis were no exception.

“They just want to buy khat,” Gerlach told me later. He was only half-joking.

After 10 days in Somalia we had almost finished our work. I needed to finish gathering material, but Ashwin had nothing to do and wanted to leave early. It went against my gut instinct, but because we had agreed to do everything together, and because it might not be safe for me to hide in the hotel by myself, I went to see him off. The road to the airport could be dangerous, so we talked to Gerlach about security: two aid workers had been kidnapped there a few months before.

Gerlach assured us that we would be safe, and his friend, the regional president, sent a personal car. A Somali gunman rode with us. But by then it was too late. We had been researched. I pieced this together only months after, when a pirate showed me an image of my own face on his phone. The pirates had pulled an author photo of mine from an old New York Times interview. I’m a dual citizen, and I had travelled to Somalia on a German passport; but they knew I was an American writer.

The first cold indication of this scrutiny came at the airport, while we sat around having tea. We had to wait for the terminal to open. One of Gerlach’s friends, Yassin, happened to mention my name, and a young Somali man glanced over from a table nearby.

“You are Michael Scott Moore?”

“Yes.”

“I have seen you on the internet,” he said. “You are famous.”

“I am not,” I said, frowning.

After a long delay, Gerlach and I shook hands with Ashwin beside the airstrip and began the drive back to our hotel. Along a dusty road, which cut between the graves of Somalis killed in the long civil war, a pickup truck mounted with a heavy gun was waiting.

The truck approached with its cannon aimed at our windshield. A dozen or so men jumped off and swarmed to my car door. They fired into the air and tried to open the door. I held it shut, but they cracked my wrist with their Kalashnikovs, pulled me out, and beat me on the head. Gerlach was also beaten – but not kidnapped – and our gunman in the passenger seat never fired a shot. My glasses were broken in the dust. My brain recoiled from what was happening. Before they fired their weapons I had convinced myself they just wanted to see my papers. While they dragged me to a waiting car I felt a reflexive horror for my family and the burden I was about to become. I wanted to rewind everything.

The truck approached with its cannon aimed at our windshield. A dozen or so men jumped off and swarmed to my car door

We drove, first, to a house on the edge of Galkacyo, where my bag was handed to an angry-looking man who waved us away. We sped out of town to the east, and I sat with ripped clothes and a bleeding scalp, squeezed into the back seat next to three surly gunmen, bouncing across the bush, for several hours.

“OK, OK,” the pirates in the front seat said to me. “No problem.”

The car bounced over a bump so hard that my head hit the roof and left a bloodstain on the fabric.

“Fuck!” I said and pointed at the blood, cradling the broken wrist in my lap.

At first I spoke mainly in obscenities.

“OK, OK,” they said.

Near sundown we arrived at an outdoor camp in a reddish, sandy part of the bush. The pirates blindfolded me and led me to a foam mattress, which lay in the open beside a crumbling low cliff. I was dazed and bloodied but aware of other Somali gunmen, and other hostages. I saw very little. Without my glasses I am drastically nearsighted, and I spent my entire captivity, more than two and a half years, in a fuzzy state of near-blindness.

The guards handed me bread, a bottle of water and a can of tuna. That would be my diet for the next several months, along with occasional cooked pasta or rice. In two months, I would lose about 40 pounds.

“OK Michael?” one of the guards said. He was an earnest young Somali with a turban and pale brown skin. He stood on a rise holding a Kalashnikov; the marbled sky behind him had thin swirls of reddening cloud.

“No,” I said after a while.

The photographer, Cynthia E Wood, posted this image in a Facebook group on Michael Scott Moore’s birthday in 2012, after he had been kidnapped. It became a touchstone image for his friends during his captivity. Photograph: Cynthia E Wood

For some reason I thought about the things in my backpack. My sense of self was still intact, like a man who’s just lost his head and wants to put it back on. “They took my bag,” I told the guard. “Can you ask someone for my bag? It’s a maroon backpack, it had a camera in it.”

“They steal your camera?”

“Yes.”

“Thieves!”

I looked at him curiously. After a while I squinted at all the guards, one at a time, to see if I recognised any from our trip to Hobyo. They were not the same men. But if they were Sa’ad pirates, it didn’t matter. My hosts – meaning Gerlach’s relatives – had turned on me.

The next morning we moved to a house. The pirates stuffed two other hostages into the car with me and bound our hands. They were both in their 60s, one African, the other – I thought – a Pacific Islander. He had cocoa-coloured skin, small piercing eyes, and twin furzes of gray hair sticking out over his ears. This was Rolly Tambara. We were about to become good friends.

We drove along the coast that morning until we entered a half-wrecked, filthy house on the edge of a town. (Later I learned it was Hobyo.) We spent three nights there in separate rooms. We sat on thin mattresses, free to walk around but not free to visit the toilet stall without permission.

A Somali man who may have been a livestock doctor came to inspect my wrist. He declared it “not broken,” although pieces of bone moved around under the skin. He sewed a thin wooden splint around the throbbing joint and said it would heal in three weeks. (It took six.) Then I tried to sleep; but before dawn Rolly and I were loaded into a Land Rover and driven across the bush by pirates who seemed to be in a nervous panic.

We drove at random, for several hours, until dark fell. What I wouldn’t learn for days was that a posse of American helicopters had rescued the two aid workers captured in Galkacyo – Jessica Buchanan and Poul Thisted, an American and a Dane – from a pirate camp in a distant part of the bush the previous night. Nine Somali guards had been killed. The kidnappers were different from mine, but the pirate kingpin Mohammed Garfanji had financed both abductions. He lost a relative in the raid.

The Somalis in the front of our Land Rover were agitated. One of them, who the men called Ahmed Dirie, had rotten teeth and brown, stained-looking eyes. He seemed to be our guards’ lieutenant – their most immediate, low-ranking boss. His face looked half-melted with anger and he kept an ammunition belt strapped around his pot belly. He and his driver, Muse, quizzed me while we drove across the desert bush.

“Are you a marine general?” said Muse.

“Me? No.”

“Colonel?”

“No,” I said. “I’m a German citizen.”

“Ya, ya, ya,” said Ahmed Dirie.

They knew I was American, and they thought the Buchanan rescue had something to do with me. But I would not learn the full story for weeks. From what I understood of Muse’s Somali, a dozen people had died in some distant town.

“Helicopters!” said Muse. “American!”

“Oh boy.”

We settled in a dusty wooded valley. The Somalis let us sit freely, like kindergarteners, on foam mattresses under a tree. The pirates had cut Rolly’s hair in Hobyo. Now he looked almost dapper, with a half-bald head and a tough, small, sparkplug frame.

On our first morning in the valley, after a breakfast of cold rice, Rolly started to talk. He was an old Catholic fisherman from the Seychelles. Pirates had caught him and his friend Marc three months earlier, in late 2011. They were cleaning fish on Rolly’s boat about 50 miles from their home port when pirates approached in a skiff under a crackle of gunfire. The Seychelles’ main island of Mahé lies about 700 miles from Somalia. Their trip to Hobyo, at gunpoint, took seven days.

Rolly spoke a comical, French-inflected English. “When I go to fishing,” he told me, “I no like to eat fish. I bring chicken, saucissons, like that.” His boat had been equipped with a stove, and on his first morning as a hostage he tried to cook pork sausage for breakfast. One of the Somalis noticed. With his bare foot, from behind, the pirate kicked the sausage overboard. His Muslim sensibilities were offended by pork sausage. Rolly still couldn’t believe it. “They catch you and take you from your home,” he said. “And then they no like what you eat.”

A view of Hobyo as Michael Scott Moore and

Ashwin Raman visited, with armed guard, in January 2012 Photograph: Ashwin Raman

The pirates had also glanced at the name printed on the rear of Rolly’s boat – Aride, Port Victoria – and declared him “Australian”. They thought that he was too light-skinned to be African. In fact he was one-quarter Chinese. “Seychelles, you know, is an island country,” he said. “We are mixed, mixed, mixed.”

Ransom for the two men was $20m. Rolly’s mind seemed to churn through the same trenches of thought every day, and while we lay under thorn trees in the bush he would wince while he made complicated calculations.

“Michael,” he would say. “You know how much is $20m my country? Is a lot of money. You can buy house, you can buy car.”

“Rolly, with $20m you can found a corporation.”

“Heh-heh,” he would say.

The nighttime raid by US Navy Seals had rescued Buchanan and Thisted in open savannah, at night, but the Somalis kept us outdoors in the weeks and months that followed. They boasted on the phone to journalists but took no serious precautions, apart from hiding us under some trees. “Holding the hostages in one place is unlikely now because we are the next target,” a pirate spokesman told the Associated Press in late January – but once we had settled in the wooded valley, Rolly and I spent weeks there, even after a treetop-stirring surveillance flight by an enormous plane (probably an American P-3 Orion).

We moved back to houses in Hobyo after three weeks only because of a rainstorm. I had the impression that the pirates were making the whole thing up as they went along. They had no clear plan to extract a ransom or hand me back. They just made outrageous demands. Garfani would ask $20m for me.

One night in late February, a month after my capture, the guards hauled me in a Land Rover, alone, to a remote part of the bush to meet the pirate kingpin. I had heard of Garfanji but never seen a picture. He was a powerful criminal, with a reputation for cruelty as well as kindness to his own men.

The person I met in the bush that night seemed groggy and dull-witted; he sat cross-legged in the dust and spoke in a high, almost childish voice. He dialled a private American negotiator on his softly glowing smartphone.

The negotiator said, “The man who just handed you the phone is Mohammed Garfanji,” and my blood felt just like ice water. “They aren’t beating you or anything like that, are they?” he asked.

“No,” I said, although one boss, Ali Duulaay, had beaten me several times. “Not systematically,” is what I meant.

The negotiator’s voice was sane, strong, even good‑humoured. I had the false idea that somebody was in control. After a brief conversation he connected me to my mother in California, and hearing her was like hearing music for the first time in weeks. But the call was fruitless, like most of these ransom conversations. The pirates wanted too much. Even the negotiator sounded surprised by “$20 million”.

After the call, Garfanji searched his phone for the sound file of a news report about the Buchanan rescue. He said, in a slurring, apathetic voice:“Your people have killed nine of my people. If they try it with you, we will shoot you.”

Your people have killed nine of my people. If they try it with you, we will shoot you Mohammed Garfanji

“What happened to the hostages?” I decided to ask.

“They were also killed.”

He tapped his phone to start the file and tossed it in the dust. I heard a clip from what sounded like an Al-Jazeera broadcast, which explained in clear English that two aid workers held captive in Somalia since autumn 2011 had been flown by US helicopters – alive – to the American base in Djibouti. My heart thumped with glee.

“I’m very sorry to hear that,” I told Garfanji, and I think no one in that circle of men had any notion how the phone had made a fool of their proud commander, by revealing his poor command of English.

When we returned to Hobyo that night, I lay awake in the darkened house, under a mosquito net, thinking about the report. Nine dead pirates would complicate negotiations. A ransom seemed hopelessly far away.

I tried to imagine a rescue in that house, which seemed to be a half-built pirate villa waiting for a last infusion of cash. The walls were half a metre thick. The windows had metal mesh screens instead of glass. A concrete wall surrounded the house. A shootout here would be ugly, I thought.

My door to the front porch was flimsy wood, and the guards sat right outside, on a woven mat. Two or three stayed up all night by chewing khat. “It is the Somali beer,” one of my guards joked.

Khat was far more important to them than the fishing war off the Somali coast. They chewed it whenever they could, not just at night. They had to be locked with me in these prison houses like hostages – a separate runner came and went with keys – so nothing excited them more than the daily arrival of fresh bundles of khat.

Most Somalis are Sufis, and chewing khat is one indulgence that sets them apart from the more puritan Salafists in the militant group al-Shabaab. Some of my guards had even fought against al-Shabaab in Somalia’s civil war that has rumbled on since the federal government first collapsed in 1991. The men considered fundamentalists to be an alien invasion force, and when they heard news on the radio about a drone strike against a Shabaab leader they would report it to me and hold up their thumbs: “America, good!”

Still, they were quite devout. Five times a day they took turns on a clean mat and mumbled a prayer towards Mecca. A Turkish naval officer once told me that pirates by definition were “not Muslim”, and he doubted they would observe Ramadan; but the pirates I met were meticulously observant.

One day I asked a guard about his beliefs. It was a bit like asking a mafia hit man why he went to church, but I wanted to hear it straight from a pirate’s mouth. “Bashko,” I said, “you are a Muslim.”

“Yes!” He was proud.

“But you are also a thief.” I bumped my fingers together, which had become a comprehensible gesture for us. “No same-same.” These things don’t fit together.

A smile crept over his face as it dawned on him what I’d said. He laughed and rattled a translation to the other guards. He straightened up in his chair and tapped his chest.

“I am a Muslim,” he said. “But I am also a thief,” he admitted. “Why? Because in Somalia, hungry-problem.”

“Yes, that’s true.” I held his eyes. But, I added, “I don’t think Islam works like that.”

Bashko was my friend among the guards, a quick-minded, bantam kid in his 20s with clever eyes and a flashing smile. I wanted long, detailed conversations with him – I wished intensely for a translator – but with our pidgin mix of English and Somali we could only speak in broad terms.

The theological problem nagged him, though, and after a week or two he answered my question. He said the Koran called for struggle against nonbelievers. Thieving from infidels therefore was not theft.

“Really?”

“Jews, Christians, Buddhists …” OK to steal from them, he implied. “Muslim, no.”

I shook my head. “Does the Koran say you can also kill infidels?” I asked.

“No.” Bashko was adamant. “All life is sacred under Allah.”

One sura, 9:5, the so-called Verse of the Sword, does mention kidnapping, and it is often used as an excuse for hostage-taking and even violent jihad. But we had no Koran in the prison house. In fact, I rarely saw the men read.

“But under Allah,” I asked Bashko, “it’s OK to steal from other faiths?”

“Yes, it’s in the Koran,” he said, and smiled, as if to say there was nothing he could do; the book outranked us both.

For the first few months of 2012 we heard regular surveillance in the air, and the roar of a low-flying Orion plane, every few days, would give me a thrill of reassurance and hope. It had the opposite effect on my guards. They wanted me to keep my mouth shut every time a plane came near because they thought sophisticated American listening devices could locate the sound of my voice.

In late March, members of the same pirate gang hijacked a long-line tuna ship off Somalia and anchored it near Hobyo. Rolly and I had to move onboard in mid-April. The idea, I think, was that US helicopters would be less likely to descend on a rusted industrial fishing boat filled with two or three dozen hostages than they were on a house or a camp in the bush. A few weeks later, in May, the pirates moved us back to land for 24 hours. A gap-toothed and rather stupid pirate called Bakayle said we were about to receive our “plane tickets” home. That was a bitter joke, and what followed would shape up to be the strangest and most appalling day of my life.

A Somali pirate in front of a hijacked fishing boat in 2012. Photograph: Farah Abdi Warsameh/AP

We drove through the dry bush for an hour. The cars made their way to a sloping wooded area where other cars, and other Somalis, waited under the trees. The men marched Rolly away behind a thicket. I felt uneasy, but the pirates said, “No problem,” and I sat with them for about 20 minutes until one rolled down his window and we heard a harsh voice cry out.

“Rolly!” the guard said. They led me to a cluster of tangled trees. I saw a group of men, heavily armed, with rocket launchers and AK-47s, standing or squatting in the dust. Some wore turbans and keffiyehs. Most were older and seemed to be ranking pirates or clan leaders. They watched me with wary eyes for a reaction, like large predatory cats. Rolly dangled upside-down from a tree. They had tied him by the ankles to a heavy bough. He swung free in nothing but a pair of cotton shorts; his arms flopped like a rag doll’s. A fat, deep-black man with a high voice whacked him on the chest and feet with a bamboo cane.

Rolly dangled upside-down from a tree. They had tied him by the ankles to a heavy bough

It was a torture scene from the days when Ottoman officials would tie the feet of criminals and subject them to “bastinado”, or public foot-whipping. Two teenagers filmed it. Other Somalis ran up to kick Rolly in the ribs. They seemed to enjoy themselves. But Rolly didn’t scream again. He just closed his eyes and let it happen. I wondered if he was in shock.

The fat man was Mohammed Garfanji. He handed his cane to another Somali and came up the slope, where he squatted some distance from me and squinted.

“Hello, Michael. Do you remember me?” He said Rolly had to be punished because he would not admit to being Israeli. “But he isn’t Israeli,” I said. “I have found proof on the internet!” Garfanji blustered.

The man now holding the cane slid it through the cotton knot at Rolly’s feet and used it to turn him this way and that. Other men kicked him. I was about to say that Rolly spoke no Hebrew; but that could have led to an awkward line of questioning. (“Have you been to Israel?” etc) The Somalis I met harboured an unquestioned hatred for Jews.

I decided to say, “He speaks like a man from the Seychelles.”

A more junior boss, Ali Duulaay, squatted next to Rolly in the dust with a lit cigarette. Duulaay had organised both of our kidnappings. Garfanji was the financier, as far as I understood – he sat at the top of a number of interrelated pirate gangs – but Duulaay was a direct gang leader.

He liked to use his fists, and he’d clobbered Rolly and me several times. He was lean but strong, about 40, with acne-marked skin. A little game occurred to him now. He held the filter end of his cigarette up to Rolly’s upside-down face and taunted him. “Come on, Rolly,” he seemed to be saying, with a smile, trying to slip the cigarette between Rolly’s lips. “No, Ali, you know I no like cigarettes.” His face looked strained and flushed.

Bakayle taunted the old man about his ransom. “We will get $50m from your family!” Rolly didn’t answer, but from that day onwards he would refer to Bakayle as “Fifty Million”. At last the pirates lowered him to the ground. He lay on his side, propped up on one elbow, to recover his breath. I went to sit near him and asked the Somalis for food and water. One guard brought a bottle and box of cookies. “Are you hungry?” I said and Rolly nodded. “Just relax for a while. I think it’s over.”

But now it was my turn. Garfanji said “these men” in the woods wanted to know why no one had wired them money. Where was that $20m? “You’re asking too much,” I told him. “Even you know that.”

Garfanji bellowed my answer to the assembled bosses, who hollered their dissatisfaction and shook their weapons. One looked like Mustaf Mohammed Sheikh, the pirate Ashwin and I had interviewed in Hobyo. He kept a keffiyeh wrapped around his face, so it was hard to tell; but the resemblance was chilling.

Garfanji said I would be sold to al-Shabaab in one hour if the money wasn’t sent right away. “They are coming here now!” he said and I felt a mixture of fear and ashen contempt.

“Well, we don’t have the money,” I muttered. “There just isn’t that much money available.”

“You’re lying! I have looked into your bank account! I know how much money you have.”

His men had stolen a bank card, and I didn’t know whether someone had hacked my account. “So you know I don’t even have one million, Mohammed,” I said.

That tripped him up. He wanted to accuse me of having more, but from the way he dissembled I gathered he had not cracked my account.

“The American government hasn’t given us any answer,” he said. “These are dangerous men. They are not satisfied. How can we find more money?”

“You have my German passport,” I said. “Maybe the German government can help.”

I didn’t think it could. But I was surrounded by armed men and had to say something. Garfanji shouted my answer to the others and they shook their weapons. They liked the idea, apparently. Garfanji suggested a video. The whole episode, from start to finish, was pirate theatre. We rehearsed an interview while the cameramen adjusted their tripod and a handful of pirates stepped behind me, holding heavy weapons. I did not notice them at first. On the video they are hard to miss; but they were very quiet and I noticed them only at the end, after they stepped away.

Another man insisted I wear a pink blanket over my head, to disguise me from aerial surveillance, so in the video I look not just wretched but ridiculous. Garfanji played the inquiring journalist. He bellowed questions from behind the camera.

Afterwards Garfanji stood on a rise of dirt and addressed the men. He had pretended to mediate between me and this wild gang of bosses; now he rose to his true role as their chief. He swung the bamboo cane and pontificated. “Tyrant” was too big a word for him. He was a play-tyrant, a sadistic bully, and I saw for the first time that Garfanji, this high-voiced overweight child, had flecks of grey in his hair.

The camera team packed up and stopped to apologise to me. One had small wire glasses and spoke clear English. “I am sorry,” he said. “We can’t do anything. We are only journalists. We will put these videos on the internet.”

Within days, in fact, the video would be for sale. Someone sent an email to Ashwin in Germany, offering to sell the video for $2,000, but he declined. It ended up on the Somalia Report, a news site, and became the single well-known video from my time as a hostage. (We made four or five.)

“This wasn’t journalism,” I told the teenage cameramen and gestured at Rolly. “It was humiliation.”

“Humanitarian, yes.”

“That’s not what I said.”

In the autumn of 2012, eight months after my capture, I was moved to a series of barren prison houses in Galkacyo. I never saw Garfanji, or Rolly, again. (Rolly and his friend Marc were both ransomed and flown to the Seychelles in November, 2012.)

The pirate now in charge was Dhuxul, an almost bald, almost obese man with deadened eyes and a tuneless voice. He walked with a limp, on a wooden prosthetic. He told me his foot had been shot off by American helicopters during the Black Hawk Down incident in Mogadishu in 1993. That’s not impossible – he was in his late 40s, which is old enough – but the number of pirates I met with physical scars from that disastrous day of violence was implausibly high.

Dhuxul was pronounced “Duhul”, a dull and shapeless noise, not so different from the man. He lived in one of our prison homes, which was unusual. A high-ranking boss like him tended to keep his distance, rather than sleeping near the hostage. He kept alcohol and a TV in his room.

On his orders, the men chained my feet every night. Until then I had not been fettered or tied. Now, after I finished my typical dinner of boiled beans, a guard would kneel in front of my mattress and wrap my ankles in a bicycle chain. If he didn’t like my behaviour that day he might tighten the chain; otherwise he’d leave it loose. I had to be restrained all night, from about six in the evening till the morning call to prayer around five.

There was no clear explanation for this treatment, which started in the spring of 2013. But the long, 18-month period when my feet had to be chained at night remains as a sodden low point, when something crucial shifted in my spirit. I had flown to Somalia with curiosity and compassion; I had wanted to show, as far as I could, how Somalis lived and what pirates thought. With the chains on, I struggled every night with hatred and debilitating rage. The men treated me like a herd animal. Around me they smoked, giggled, and bowed to Mecca the way a nomad in the desert might pass days and nights around a camel.

At night I would dream about lively conversation with family friends, in Germany or America, but the dream always ended with the riddle of why I had to return to some kind of jail, and I would wake up to the sight of concrete walls in a house and languid Somalis sitting beside their guns.

A hostage does nothing, but the long hours are a crisis of longing. My sense of self, in fact my sanity, would surge and ebb. I tended to wake up in a stark panic and pray for no greater mercy than the dawn.

Rolly had told me that in the first few weeks of captivity, he had considered overdosing on pain pills. (He was an old man, so the Somalis had been generous with a variety of pills.) He had learned to pray on his rosary instead. I coped in other ways. I was a lapsed Catholic, but I found a Bible to read on the fishing boat. Yoga helped to calm my churning mind. I knew my family and colleagues were working to get me out, but thinking about so much money and trouble devoted to the cause of my freedom brought me close to violence.

Suicide would have been easy. AK-47s lay around like junk. When I was not with other hostages the notion of grabbing a rifle to shoot a few pirates, and then myself, began to seem not just desirable but moral. It would have saved a lot of people a great deal of trouble. It would have spared any Seal team the dizzying risk of a mission. I steered around the idea on some days only by cold logic, since killing myself would have meant a permanent loss for my family and friends.

What helped was a paradoxical attitude of forgiveness toward the guards. In different circumstances, Bashko and I would have got on well. Most of the guards, I had to remember, were just hired hands who deserved punishment far less than the bosses who had plotted my kidnapping. I also remembered a fierce American essayist called Richard Mitchell, who for some reason was on my mind almost every day. In one of his books, Mitchell revives the ancient idea, from Epictetus, that a victim suffers only by his own consent. Self-pity does nothing but heighten the pain. “To be sick, or to suffer, is inevitable,” writes Mitchell, “but to become bitter and vindictive in sickness and suffering, and to surrender to irrationality, supposing yourself the innocent and virtuous victim of the evil intentions of the world, is not inevitable. The appropriate answer to the question, Why me? is the other question, Why not me?”

That’s stoicism pure and simple. It helped in Somalia. A sense of victimhood in those prison houses was easy to contract, like a contagious disease, and remembering Epictetus – however second-hand – boiled a good deal of neurosis away.

In one Galkacyo house my mosquito tent and mattress lay in front of an open door, facing east. For months in 2013 I watched the dawn sky lighten every morning through an arabesque arch. The men watched with their Kalashnikovs from a khat-littered mat on the patio. When I stirred at night, they objected. But sometimes I had to urinate before the morning muezzin.

“Wuuriyaa!” they said one morning when I started to rise in the dark. Hey!

I sat up and lifted my mosquito net. “I have to pee,” I said.

One guard aimed a flashlight at my face. I sat still and held up my chains.

“Kadi,” I said.

The usual night guard went by a nickname, Madobe. He was a lean and sarcastic, handsome, simple-minded man who seemed to hate my guts.

“Kadi,” I insisted, although making any disturbance at night was against the rules. Madobe lurched forward through the doorway to flick his knuckle into my eye.

“Jesus!” I shouted.

The noise angered another guard. He argued with Madobe. Hitting was against the rules. They argued in whispers until one of them tossed me the padlock keys, which landed with a clink on the floor.

Madobe liked to abuse me. He was adept with his knuckle, and sometimes the eye would hurt for a day or two. That morning I decided to protest his behaviour. When the chains were off, after dawn, Bashko tried to delivered my usual bowl of beans.

Madobe liked to abuse me. He was adept with his knuckle, and sometimes the eye would hurt for a day or two

I shook my head.

“No chum-chum?” he said, using our word for food.

“No,” I said.

“Why?”

“Madobe hit me.”

Dhuxul woke up, and Bashko translated my complaint. Other guards upheld my story. Dhuxul gave the men a phlegmatic order and went out. He returned for lunch with a hot restaurant meal of spiced rice and boiled goat, in foam trays, for everyone. The men ate with relish on the patio and Dhuxul placed a plate for me near my pillow, on the floor.

The food smelled delicious, but I didn’t move. At last Dhuxul made an offer. Madobe was asleep in the other room, but he would punish him “tonight,” Bashko translated.Now would I eat?

Refusing this concession risked punishment. I had to meet him halfway. Yes, I told Bashko, after Dhuxul punished Madobe, I would eat. Not before. I pushed away the plate. We would save the rice and goat for tonight.

Dhuxul looked annoyed, but he picked up my chains from a pile on the floor and moved to the other room. I heard Madobe’s voice. The chains clinked. Big deal, I thought – more pirate theatre. Dhuxul wanted to fake me out. But the guards on the patio looked concerned. They moved aside and I saw Madobe in Dhuxul’s clutches, bent forward with his chained hands yanked behind his back. Dhuxul smacked him across the head.

Naturally my conscience was appalled. I hated Madobe, but I didn’t like to see him chained and smacked on my account. The pirates, though, were bent on acquainting me with hunger and confinement, with the prospect of death, above all with the rule of force. They were acquainting me with Somalia.

“OK Michael?” Bashko repeated.

The boss had made his concession. I had to respond.

“OK,” I said.

And Madobe quit thwacking me.

Hostages made famous by media coverage grow more expensive, as a rule, so my family made the agonising decision to keep my case quiet. It was not easy: whenever negotiations faltered, so did my mother’s faith in the tactic, and sometimes she warned negotiators and officials around her that she wanted to tell the world. The final decision was always hers, and I don’t question it. The media blackout didn’t shorten my stay in Somalia, but my guards did listen to the radio like eager kids after each video we made in the bush. They wanted to hear my name on the BBC, and it frustrated them to hear nothing.

“Michael!”

Bashko came to me one day in 2013 with some hot news.

“America – no ransom!”

“No, they won’t pay.” I shook my head in agreement.

“Why?” he chided. “America no money?”

He had honestly expected a ransom from Washington. The optimism made my head swim. I thought it was well known in kidnapping circles that the US and British governments paid nothing (normally). Garfanji should have known it before he financed my capture; Bashko should have known it by now. After I went free, at least one FBI agent would express real surprise that these men were so ignorant of US policy. But Bashko hardly knew the difference between Britain and France.

Months passed, then years. The bosses thought I could make them rich while I slept in their houses in chains. They hit up every conceivable source of cash – governments, families, employers, institutions of any kind. The demands were outrageous, fanciful, and for a long time I sensed negotiations had stalled. During one rare phone call with my mother in 2013 I blurted in German that a rescue “would be welcome”. By then I didn’t mind getting killed. For Bashko it seemed the height of western evil that helicopters might arrive before a fat sack of money; but I was numb to the risks of a rescue, and I imagined, naively, that the US no-ransom policy would require a consistent military response.

My case was particularly difficult. Two governments had to be prodded for help; two governments had to jostle for command. I spent 32 months as a hostage, and it is possible that the oscillation between US and German responsibility lengthened my time in Somalia. In the end I owed my freedom to a ransom cobbled together by my family and a number of US and German institutions. But it came without warning. I suppose Bashko did try to tell me; but rumours of a ransom surfaced every month, and I quit paying attention. The pirates’ wispy gossip and promises of freedom were more maddening than the raw passage of time, so I learned to listen to them with distant bemusement, the way an old man watches TV.

The morning of 23 September 2014, was not unusual. I woke up in the dark and waited for a guard to toss over keys for my chains. I undid the padlocks, went for a piss, and came back to face a sullen bowl of beans.

After breakfast I had a phone call with a mysterious American negotiator named Bob. My Somali translator, Yoonis, let me talk for 30 seconds before he yanked the phone from my hands. Bob managed to explain exactly nothing. Yoonis said, “Proof of life, only!”

Around noon I had to use the toilet, and from the high, broken-tiled, sun-shot bathroom I heard the front compound gate open for a car. That was strange – cars came at night, as a rule. A young Somali named Hashi stood outside the door with his gun.

“Michael? Gari,” he said.

Michael, your car is here.

“What gari?” I said. “I’m busy.”

“No problem.”

When I came out the men were buzzing with enthusiasm. Three Somalis were showing off a clear plastic sack of bound hundred-dollar bills. The bag was sealed. I couldn’t tell if the bills were real. “You are going free!” they said, but I didn’t believe it. I’d heard it too many times, and I had grown stunted and cramped. My brain felt like a fish in a swamp.

“You must pack your bags,” one of them said. “You are going to the airport.”

I had a bag of dirty clothes. I threw them together but still wasn’t convinced. I climbed into the car with two men, Yoonis and another translator. Normally eight armed men crammed me, blindfolded, into the car. Not this time. We drove through Galkacyo – what the men had called Haradheere for almost two years – and out some distance into the bush, where another car waited.

“Get out,” said Yoonis. “You are free.”

I felt bewildered but I climbed into the new car and found myself alone with a strange driver, a Somali who spoke American English. I was still convinced that the promise of freedom was false. But, to my surprise, he dialled a number while he drove, and on the phone I heard not just Bob, the negotiator, but my mother.

“Where are you?” I was astonished. “Not in Galkacyo.”

“No, we’re in California,” my mother said.

“Your driver will take you to a hotel,” Bob explained, “and another Somali will drive you to the airport. Your pilot’s name is Derek.”

Michael Scott Moore. Photograph: Christian Jungeblodt

Galkacyo’s decrepit Abdullahi Yusuf International Airport, to the northeast of town, was just a dry airstrip with a few low buildings, just like I remembered. Now, on the asphalt, a small single-engine plane waited. Next to it stood Derek, a short leathery man in mirrored sunglasses – a bush pilot. When we pulled next to the plane and I opened the door, he stood under the wing to snap a photo.

“For your mother,” Derek said in a British accent.

He shook my hand and gave me a backpack stuffed with clothes. Derek said he would deliver me to Mogadishu. From there I would take an American C-130 to Nairobi. Relief was not quite the word – I was still too shattered to feel relief or excitement or joy – but it seemed incredible to me that Derek could fly a plane. He was the first competent man I’d met in a long time.

I fastened my seatbelt. Derek climbed in and slammed his door. A Somali on the tarmac asked us to wait “just half an hour.”

“What for?” said Derek.

“A journalist is coming, he wants to take your picture,” said the Somali.

“No,” I told Derek.

I wouldn’t hear for days what was happening in another part of Galkacyo. The ransom had to be divided. Everyone connected to me would want a cut of the money. Within two days several ranking men – including Dhuxul, Ahmed Dirie, and Ali Duulaay – would sit down for a tense meeting in front of a house belonging to a boss called Nuur Jareer. Duulaay and Ahmed Dirie, my kidnappers, wanted a large share of the $1.6m ransom. But by now they were outsiders. They had to demand the money from the sub-group that held me, which included Dhuxul and Nuur Jareer.

Guards at the meeting aimed their weapons in a complicated Mexican standoff, for mutual security, according to people who described the scene to me later. But the man aiming at Duulaay pulled his trigger. Gunfire unleashed by the others killed Ahmed Dirie, his brother, and one more of my kidnappers. Duulaay died on the spot. The boss Nuur Jareer was injured. Dhuxul pulled him to safety, but within another three days Nuur Jareer would die of his wounds.

Later I heard the group had invested $2m to hold me. The top men must have been sorely disappointed.

The plane began to roll forward. Derek and I put on headsets. “Galkacyo tower, Galkacyo tower,” he radioed. “Request permission to take off,” he said and gave his call sign. “Two souls on board.”

No response. Another delay.

“Sometimes they don’t answer,” he mumbled.

I was still shell-shocked and confused. My brain would feel cramped for months. People say, “You must have been overjoyed,” but any ransom is a filthy compromise, and I had long ago given up on hope as a dangerous indulgence. As the plane moved forward, I looked at the cracked and sun-beaten white buildings of the airport – these objects of fantasy for two and a half years – with mute animal wonder.

“Galkacyo tower, Galkacyo tower,” Derek repeated. “Request permission for takeoff. Two souls onboard.”

At last there was some noise from the radio.

“Yes, OK,” crackled a Somali voice, and Derek lined up his plane.

• Follow the Guardian Long Read on Twitter: @gdnlongread