We finished the interviews, deep in the Baghdad slum known as Sadr City, and the Guardian's two vehicles started heading back to the hotel. The street was deserted until three cars, including a police Land Cruiser, sliced around a corner and into our path. Gunmen piled out and surrounded us.

One pistol-whipped Safa'a, the driver, spraying his blood on to my lap. Another wrestled the translator, Qais, out of the door on to the ground. Another pumped three bullets into the windscreen of the follow-up vehicle, narrowly missing the driver, Omar.

It was 2.15pm on Wednesday, and a moment I had dreaded since moving to Iraq nine months earlier had arrived: kidnap. A potential death sentence for Iraqi staff as well as the foreign correspondents who are the targets. Since hostages started having their heads sawn off we have all been obsessed by it.

In agreement with my Iraqi colleagues, the plan, if cornered, was for me to leg it. With a gun at my head that was not an option. I was bundled out and thrown into a Honda. I glimpsed Omar sprawled on the ground, an AK-47 trained on him.

We sped away, the Land Cruiser leading. A man in police uniform in the front passenger seat pointed a pistol while my neighbour in the rear seat handcuffed my wrists behind my back and shoved my head into his lap. "OK, OK," he said. It was not OK.

Angling my head it was possible to see sagging powerlines, crumbling houses, sheep grazing on rubbish, traffic. I waved a foot to try to catch the attention of a trucker. It was rammed back on to the floor. The driver, stocky and stubbly, turned with a toothy grin and said "Tawhid al-Jihad". Otherwise known as Abu Musab al-Zarqawi's al-Qaida in Iraq, the beheaders of Ken Bigley. I stopped breathing.

We pulled off the road and, within sight of traffic, had a 10-minute pitstop to change. A different car and different clothes. I stripped naked and was handed a brown T-shirt and a pair of stonewashed fake Versace jeans with no button. "More Iraqi, good, good," said one man. I was left barefoot. We rejoined the traffic. Documents and a copy of Iraq's draft constitution poked from the pocket of the front seat, suggesting this was a newly stolen car. The kidnappers relaxed. One lit a cigarette and flicked through my documents.

"Irish. Journalist. Not British?" He shrugged. American helicopters buzzed overhead but however hard I visualised it, no Rangers came shimmying down on ropes.

The front passenger turned and indicated his colleagues. "Ansar al-Sunna." The bad news was that that was the group that killed an Italian journalist. The good news was that this contradicted the driver. I suspected - hoped - they were winding me up.

The headcutters are Sunni extremists but Sadr City is Shia, a rival Islamic sect, and the fiefdom of the radical cleric Moqtada al-Sadr.

We had gone there to follow Saddam Hussein's trial on television at the home of a family persecuted by his regime. The kidnappers had learned of our presence and lain in wait.

We pulled into the walled driveway of a smart two-storey house. The vehicles left and the house owner, a medium-built man in his late 30s, took over. A portrait of a Shia imam gazed down from the wall of the living room, which was bare save for rugs and cushions.

As the man I would come to call Haji, a term of respect for Mecca pilgrims, sifted through possessions taken from my car, I asked about my colleagues. He examined a notebook spattered in blood. "They OK, no problem."

He said I was to be exchanged for a Shia militiaman jailed by the British in Basra, the spark for last month's violent protests. I wanted to believe that but feared being sold to the highest bidder.

There was a rumour that Sunni groups were back in the market after a lull in hostage-taking.

A separate set of metal cuffs clicked on to my wrists and I was led into a hallway. Beneath a stairwell there was a black cavity, an entrance to an unlit concrete passageway five metres long, one metre wide. A rug and a pillow were laid out.

The door clanged shut and a lock turned. Pitch blackness and silence. Going by previous hostage cases, this could be home for months. Still, no bag over the head, not chained to a radiator - could have been worse.

I sat down and tried to remember why I volunteered for Iraq. Curiosity, ambition and hoping to clear my head after a broken relationship, among other things. It wasn't feeling clear now. No story was worth this. In any case I'd missed the story - Saddam could have broken down and pleaded guilty for all I knew.

Hours passed. I pictured news of my abduction reaching family and colleagues. Not a happy image so I thought about my cat, Edward. Insects crawled up my leg. Dusty Springfield crooned in my head. Who invited her in?

Sounds of domesticity reverberated through the concrete. A woman's voice. Children running and laughing. Pot walloping in the kitchen. The television blared. Egyptian comedies, it sounded like; Haji's family laughed long and loud.

After fitful sleep the door banged open. "Morning, Rory," smiled Haji. After being allowed to use the toilet and shower, with cuffs removed, a younger man provided pitta bread, jam, cheese and sweet tea in the living room. "You on al-Jazeera, BBC, everywhere," announced Haji, chuffed. I was a celebrity. Great, get me out of here.

Cuffed again and back in the gloom, it occurred to me that the British government's official position was not to negotiate with terrorists. Fingers crossed for the Irish government.

Children banged on the door and took turns at holes in the chipboard to peer at this exotic, valuable pet who could not be allowed to stray.

Unleashed for supper, feeling stiff and sore, desperate to lengthen my time out of the tomb and provoke dialogue, I obtained permission to stretch and do press-ups. Haji grinned and took a photograph. The children loved it. The pet does tricks!

Momentarily more host than captor, Haji fetched an English-language version of What is Islam, a summary of the faith by the late ayatollah Muhammad Shirazi. He appeared not to have read up on 60 things forbidden by Islam, pages 38-41, which include a ban on imprisoning someone unjustly.

Back into the passage for a second night. Then, Haji's mobile rang. A murmur, then laughter. Minutes later the door swung open. I was going home, he said. In the boot of his car. A moon hung high over Baghdad as I clambered in.

After 20 minutes of bouncing over potholes I feared I was en route to another gang of kidnappers, my buyers. I found an oil spraycan. The plan: zap their eyes and sprint.

We stopped. The boot opened to reveal a police pick-up truck with a mounted machine gun. Real police. Haji shook an officer's hand, nodded at me and drove into the night, apparently a free man.

Ahmad Chalabi, the deputy prime minister, waited with a smile at his palm-fringed compound. Elements of Moqtada al-Sadr's movement had snatched me, ostensibly to gain leverage for friends detained by the British in Basra, he said, though some wanted to sell me to jihadists.

He said his lobbying had clinched the release. "We got you out just in time." It was over. I slumped into a seat. An aide fished a can of beer from his jacket pocket. "I think you'll be wanting this."