While reporting the war in western Libya, award-winning Guardian correspondent Ghaith Abdul-Ahad was seized by Gaddafi's militia. Here he describes two weeks inside the regime's brutal prison system

We ran into Gaddafi's troops on the outskirts of Zawiya, less than a mile beyond the last signs of rebel activity: a destroyed checkpoint, a bullet-ridden building and five burnt-out cars.

The soldiers were blocking the main highway to the coast with pickup trucks and armoured vehicles, so our driver took to the desert, skirting the roadblock in a wide arc before cutting back to the road. He was edgy after that, spooked even by the sight of a distant abandoned car parked in the middle of the road.

We – the Brazilian journalist Andrei Netto and I, travelling in the company of rebels from western Libya – would not be able reach Zawiya that night as planned. Instead we made for Sabratha, 12 miles to the west.

It was clear that Sabratha had been reclaimed by Gaddafi loyalists. The police and intelligence service buildings were charred, but they had new green flags of the regime flying above them.

We separated from our rebel escorts and took shelter in an empty half-built house, away from the militiamen roaming the streets. Later that night we saw four men approaching, dressed in dark tracksuits and carrying sticks except for one, who had a gun. When they surrounded the house there was no way to escape. They took our phones then frogmarched us, heads down, to an SUV, ranting as we went. "You sons of bitches! You Jews and Zionists! You Arab traitors! You want to topple Gaddafi? We will rape your mothers! Gaddafi will show you!"

I was put in the pickup first, then Netto. As he was getting in, a tall militiaman swung a metal pipe that struck him on the head. Inside the car the man sat behind us, jabbing at us with a stick as he continued his tirade.

We were taken a short distance to a compound guarded by armed men, where we were interrogated, then blindfolded and driven for two hours to a prison that I now know is in Tripoli. We were separated there; I have not seen Netto since. Still blindfolded, I was interrogated for four hours about my "collaboration" with the infidel British newspaper the Guardian. Then they walked me downstairs to the cells.

They removed the blindfold in a neon-lit corridor lined with 20 great iron doors with sliding bolts and white numbers. Each door had two small hatches, at the top and the bottom. Empty cartons of juice, plastic packaging and trash were piled up outside the doors.

I was pushed into cell 11, a windowless box, 2.5 metres x 1.5 metres, painted dark grey and lit by a weak bulb. The room contained a dirty mattress, blanket and soiled pillow. A low wall separated a broken toilet seat covered with a thick brown crust, a tap and a bucket. There was a strong smell of sewage.

It was Wednesday 2 March. The prison would be home for a fortnight.

Day and night in the prison, bolts were pulled, doors slammed and guards, in combat trousers, T-shirts and trainers, shoved shackled prisoners in and out of the cells.

One guard in particular – a tall man with rimless spectacles whose civilian clothes implied rank – spoke the most. "All the people we are capturing are al-Qaida infiltrators," he said at one point. "Al-Qaida are beheading civilians, burning them and eating their hearts."

Another day, he delivered a paean to Colonel Gaddafi. "We love him," he said, rolling his eyes until they were just two white slits. "We love, love, love him! And we want him. It's up to us Libyans to choose him – not the west.

"With him we have survived so many things. So many crises have passed and we will survive this. It's history we have with him. It's 42 years! I have known nothing but him and they want us to turn against him now. He is not just our leader, he is a philosopher and a thinker. He is everything."

Worse than the guards, the fear and the smell were the ravings from a prisoner down the corridor. This man's shouting, made incomprehensible by being delivered through his hands or a blanket, echoed around the jail day and night. Sometimes he would break off, a moment of silence would ensue and he would begin crying and squealing in apparent pain.

When a guard passed by he would ask in a very polite voice: "You are not serving tea or coffee today?" "We are not getting newspapers today?"

Days later I discovered that he, like many of the others, was being regularly interrogated and beaten.

In the early hours of Sunday 6 March a gunbattle began outside the prison. It started with a few bursts of small arms fire, then came the deeper note of anti-aircraft guns, which turned into a continuous long drumming. At one point guns were being fired from somewhere just next to the cells.

The inmates became excited. Were the rebels storming into the prison? Had the uprising reached Tripoli? Were we being saved? The raving man gave a long, ululating victory cry while the prisoner in cell 12 continuously repeated "O Lord" like a mantra.

The sounds of shooting rose and fell for more than half an hour before fizzling away and finally stopping when two helicopters came circling overhead.

The officer with the rimless glasses came through the corridor later, fuming with anger. He shoved breakfast through the door hatch. "Those filthy Europeans, we will crush them with the tips of our shoes," he said. "If those rebel dogs come here to attack we will all die together. The sons of Gaddafi will never run. A man lives once and dies once, so better die fighting."

On the evening after the battle the cells began to fill up. There was a man from Zwara, another from Zawiya, and a chubby grey-haired man named Richard who spoke English with an American accent. By Monday some cells had three inmates. "Why am I kept here?" I overheard one man say. "I have handed myself in after the amnesty."

"Sure," laughed a guard. "We will take you to a five-star hotel very soon."

I was moved to a bigger cell upstairs. I could still hear the doors slamming and the man shouting and the new cell was also windowless, but it was whitewashed and lit by neon night and day.

Later I heard the first of the voices coming through the wall. The cell was next to two interrogation rooms, where men were brought throughout the day. Each interrogation began and ended with the clinking sound of a man in shackles walking to or from the room. The madman was brought for interrogation at least twice.

I heard snatches of shouted questions or accusations from the interrogators – "Qaida", "attack Libya", "Muammar", "who are they?" – punctuated with smacks and thuds, like someone throwing sacks of rice at a wall, and the sound of prisoners pleading, screaming and weeping.

One interrogation on Wednesday evening went as follows:

"Stand up!"

Smack came the sound. Smack. Smack.

"I said stand up!"

Smack. Smack.

This cycle was repeated five times.

Somewhere down the hall a TV blasted pro-Gaddafi marching songs.

On Thursday 10 March I was taken out of the big cell and put in cell 18 in the downstairs corridor. This was also dark, tiny and filthy, but this time I was to share with another prisoner.

He was sitting on a torn mattress, his back resting against the wall and his legs covered with a dirty yellow and red blanket. His hair was slicked back and a few days of white stubble sprouted from his chin. "Bangladesh," he said, pointing at himself. He was shivering in a thin shirt and after few minutes of silence he added, in Arabic: "Cold. All clothes with them."

He told his story in broken sentences. He had lived in Dhaka with his wife and three children. Some years ago he had gone to "a big manager in big glass building with a big office" and paid money to get a visa to Saudi Arabia to work in construction. He had been promised a good salary, but the visa never came. After five months he was told there was no visa for Saudi, but he could get a visa for Dubai. So he paid the manager more money and waited.

Two months later, he was told there would be no visa to Dubai but there was one for Libya. "The manager said Libya is like Dubai, lots of petrol and a good salary." He arrived in Libya on a tourist visa that soon expired and the work permit and job he was promised never came, but he worked anyway, on building sites in Benghazi, then Tripoli.

When the fighting and demonstrations erupted and foreigners started leaving Libya, he asked his Libyan boss to pay him the money he was owed so he could leave the country. "He said 'later, later'."

While his friends all left for Tunis, he stayed to wait for his 800 dinars (£400).

"Four days ago" – he counted them out with his fingers – "a soldier stopped me and said where is my visa. I said I had no visa. They beat me and brought me here.

"Everywhere Bangladesh worker go, India, China, Indonesia … only here in Libya they do this to you and put you in a locked room." He crossed his hands to indicate handcuffs.

A week had passed and he hadn't been able to speak to his wife. "What is she to do now?"

Later he asked what would happen if he found enough money to get a ticket to Bangladesh: "Would they let me go?"

The following day I was moved into solitary again, but towards the end of the second week I noticed small differences in the way I was treated. On day 12, a guard brought a toothbrush. On day 13, a bar of soap and shampoo arrived. On day 14 they brought a cup of coffee and even offered a cigarette.

There was no information about what was happening outside or why I was being held, despite being told when I was first interrogated that I would be released the next day. When, I wondered, might they come and take me to the room where the beating took place?

On Tuesday night a smiling officer came to say I would be released. I was blindfolded and taken to a bathroom with a mirror, given a razor and told to shave. I did not want to shave. I pleaded with him and he relented. An hour later I was told my release had been postponed.

The next morning, Wednesday 16 March, I was given my notebooks and camera and blindfolded again. I had to lie in the back of a van and was driven for half an hour before being led into a room. When they took off the blindfold, I discovered I was back in my cell. "We made a mistake," said an officer as he locked the door.

Two hours later I was blindfolded and bundled into the van again. I would have to face trial, the officer said. There was an armed guard in the van.

The van stopped and the guard told me to move closer to him. He took off the blindfold and I could see we were outside a grand building. A second man came and led me up some marble steps.

At the top I found three colleagues from the Guardian waiting to receive me and take me out of Libya. The Brazilian journalist Andrei Netto, they said, had been released six days earlier.