It is 2 May, my twelfth full day in Misurata, and I'll start with a man I met at a private clinic that had been turned into the city's main trauma hos­pital. The uprising against Muammar Gaddafi was two months old. Loyalist forces surrounded Misurata and controlled parts of the city centre, but the thowar - or revolutionaries - were putting up fierce resistance despite being outgunned. The battle crackled and boomed day and night.

Dr Tahar Alkesa, a surgeon, was sitting on the curb outside one of the white tents erected in front of the clinic to serve as a makeshift emergency ward. He is 31 years old and undoubtedly handsome, but the hours and stress had marked and changed him. He was sallow and unshaven, with dark rings under his puffy eyes. The evening light was soft and fading fast as we chatted. He rubbed his arms for warmth.

I had seen Alkesa at work earlier in the day, when fresh casualties were arriving at the hospital every few minutes. An ambulance or pick-up truck would screech to a halt outside the tent, amid cries of "Allahu akbar". If the victim was a thowar, he usually had a bullet wound, having been picked off by a sniper. Civilian casualties generally had shrapnel injuries caused by shells or missiles, the most vicious of which was a Grad, a long, tubular projectile fired out of a 40-barrelled launcher known as "Stalin's organ". When they were fired into Misurata, you heard a whoosh, whoosh, whoosh, and then bang, bang, bang.

One Grad victim arrived in the back of a blue sedan. Both his legs had been blown off at the knee. A crimson stream of blood trailed on to the tarmac as he was carried into the tent. Within a few minutes he was wheeled out, covered by a blanket. People gathered outside and launched into an anguished but beautiful refrain: La ilaha illa Allah,/La ilaha illa Allah,/ al-shaheed habib Allah. ("There is no God but Allah, there is no God but Allah, the martyr is dear to Allah.")

Alkesa worked without interruption, stitching, cleaning, talking softly to the patients, offering words of reassurance. His expression changed little, even when a grimacing man in his mid-twenties was rushed in. The man's face was blackened by smoke and his eyes were white and wide with pain and terror. His filthy khaki pants were bloodstained and torn. His forearms were shredded. He was a tank driver. The thowar did not have tanks.

His first request was for a lethal injection, because he was convinced that he would be tortured or beaten for fighting for Gaddafi. Alkesa politely said no, assuring him that he would be looked after. He cleaned the man's leg and groin wounds and sewed up the strips of flesh on his arms. The tank driver said he was from Tripoli, and that his commanders had told him that Misurata was under the control of foreign-ers and terrorists who had been destroying mosques. He said he felt he had been cheated, and was sorry.

Now, sitting on the curb, Alkesa told me: "Inside me, I really did not want to look after that man. I did not enjoy treating him. But it was my duty to look after a human being."

It pained him even more that the tank driver was a Libyan, unlike those of Gaddafi's forces, a minority, who are mercenaries from elsewhere in Africa, usually Mauritania, Chad or Sudan. "I just ask myself, what has Gaddafi done with their brains to make them fight us like this? He is not a human being. He is evil. Satan."

Until a few days earlier, he had not seen his wife, his four-year-old daughter and one-year-old son for a month. So intense was the workload at the hospital that he had been sleeping there; his family had become trapped after government troops overran his neighbourhood. Because the mobile-phone networks in the city had been cut, he had no way of reaching his family. "For two or three days I was completely dissociated from this world," the doctor said. "Even though I was working, I was asking myself: 'Is this real? Am I even real?' Then I came round and started feeling myself again."

Still, he said, every night when his shift ended, he would walk to his car, open the door and sit in the driver's seat. He had nowhere to go. He just needed a private place to weep.

Misurata is Libya's third-largest city, about 200 kilometres east of the capital, Tripoli, where the Mediterranean coastline dips south in the Gulf of Sirte. In better times, if Gaddafi's rule in peacetime could be described that way, you would drive there from the capital with a government-approved guide by your side. But since the beginning of the revolution, the only way in to Misurata has been by boat. First, you fly to Cairo and drive west for 14 hours, crossing the Libyan border roughly halfway. That gets you to Benghazi, the eastern city where Libya's revolution began in mid-February.

From there, you travel on a local fishing boat, carrying emergency supplies and most likely weapons for the rebels. The voyage takes over 40 hours.

Another way in is on the Ionian Spirit, a Greek ferry chartered by the International Organisation for Migration to pick up foreign workers stranded in Misurata. That journey takes just under a day, assuming loyalist forces are not bombing the port - as they often were.

Misurata has a proud history. An important trading post since ancient times, it provided determined resistance to Italian occupation a century ago, prompting one commander to declare that Libya was a snake and Misurata its head - something local people love to tell you. In modern times it became Libya's industrial hub, with its port and one of Africa's largest steel factories. Low-slung and sprinkled with palm trees, the city is well laid out and mildly prosperous. Most of the 300,000-plus residents had enough food to eat and many had cars and decent houses, too. Yet, given the country's vast oil reserves and small population, Libya should be much wealthier, more akin to Dubai or Abu Dhabi, in the view of many people you meet.

Since seizing power in a coup d'état in 1969, Gaddafi has squandered tens of billions of dollars on vanity projects and misadventures, such as sponsoring international terrorism. Meanwhile, countless public works projects, such as the renovation of Misurata's main hospital, were allowed to drag on for years. Yet that was not the main reason Gaddafi was so despised here, Alkesa told me. He explained that by the time he was born, in 1979, Gaddafi had come up with his "Third Universal Theory" of government, which he claimed was superior to democracy and communism and would lead to "a state of the masses". Its principles were laid out in his Green Book, which became required reading in schools and universities.

Most Libyans thought it was quackery, but very few dared question it openly. Those who did so were hanged. As a public service, Gaddafi ensured that the executions were shown on state television. "I remember watching them as a child," Alkesa said. "Some loyalists would run up to the bodies as they hung and jerk them downwards, to make it more violent. My father would have tears in his eyes when he saw that. That is why we have always hated Gaddafi. Not because we lacked money or food, but because we had no freedom . . . We also believed that nobody could destroy him. We were resigned to waiting for God to take his life."

Then, in January this year, there was a revolution in Tunisia, which borders Libya to the west. And then the turmoil in Egypt, to the east. The despotic leaders of both countries were toppled by people power. Libyans were inspired, especially the youth, but still they had no idea how they could emulate their Arab neighbours, Alkesa said. Compared to Libya, Egypt and Tunisia had seemed like liberal democracies even before their revolutions. “Despite our dreams, nobody could imagine that this could happen in Libya," he said. "No one. Really, no one."

On 15 February, there was a small protest in Benghazi over the arrest of a lawyer representing victims of a prison massacre. Two days later, a protest had become a city-wide uprising. The ripples reached Misurata. Nothing had happened yet, but people sensed it might. Alkesa's two brothers, who sell gold jewellery, removed all the stock from their shops and brought it home. Something was about to happen.

On my arrival at the port in Misurata on 20 April, I was taken to a girls' school that had been turned into a media centre for local journalists, some of whom accompany the thowar to the front line each day. They post video footage on YouTube, or send it to al-Jazeera, to which every television set in the city appears to be tuned.

At the media centre, I met a 23-year-old man whom I'll call Ahmed Ali. He worked in the graphic arts and he spoke good English. He was one of Misurata's first revolutionaries. He told me that, on 17 February, he and a few dozen other young men, most of them in their early twenties, held a demonstration in support of the people of Benghazi.

They were arrested by the security forces, who beat them before hauling them away. "During interrogation they showed us our Facebook pages, where we had been talking about plans for a protest. They had been watching us even before," Ali told me.

Some of those arrested, including Ali, were held overnight, others for two days. It was the spark that Misurata needed. The editor of a newspaper where Ali sometimes worked announced he would not publish again until all the men were released. On 19 February, some of their families and friends went on to the streets to demand the same. “We were 30 people, and then in a few minutes we were 100. Soon we were 5,000," Ali said. "It was incredible."

The security forces opened fire. The first martyr of the revolution, Khalid Boshahma, was shot dead. For his funeral the next day, tens of thousands of people turned out in the city centre. Tear gas was used. Snipers who had been positioned in nearby buildings began firing in the air. People in the crowd started hugging each other, believing the army had taken their side by refusing to shoot at them. But then the snipers started picking people off. Dozens were shot in the head or chest. None of the protesters had guns - keeping a weapon was prohibited in Libya under Gaddafi - but their rage was enough to shake the army. As demonstrators began setting fire to buildings associated with the regime, state security hastily left Misurata, perhaps having been ordered to, or maybe out of fear. Tension was mounting in Tripoli, and so the government was unable to spare troops to mount a counterattack for two weeks. For many people in Misurata, it was the best fortnight of their lives, Ali told me. But they knew Gaddafi would be back.

Under the guidance of a hastily assembled judicial council, the people of Misurata prepared to defend their city. By looting the local armoury, they had acquired some AK-47s and grenade launchers but most of their weapons were home-made. Young men were instructed to prepare thousands of Molotov cocktails as well as fist-sized bombs known as gelatina, made from TNT.

When Gaddafi's forces finally attacked on 6 March, they met no resistance and were allowed to drive into Tripoli Street, the main boulevard, a few miles long, with its smart shops, coffee houses, banks and office blocks. Then, when the order came, hundreds of young men positioned on the rooftops along the street started hurling their bombs. The thowar joined in with their light weapons. Taken by surprise, the loyalist forces battled

for four hours to fight their way forward, but could not. Many of Gaddafi's soldiers were killed, and the survivors were driven back to the edge of the city.

The next attack, on 19 March, was on a different scale. Troops entered the city from several sides, Russian-made tanks leading the way. This time they forced their way into Tripoli Street. Out of armoured personnel carriers poured many hundreds of snipers, who raced up into Misurata's office buildings and residential apartment blocks.

Other units took over the city's vegetable market, the college of medical technology and the unfinished hospital. The urban conflict had begun: terrifying, old-fashioned war where men fired at each other at close quarters. The daily casualty count rose remorselessly. Ali's maternal uncle was shot in the leg by a sniper. One paternal uncle was killed. Another was kidnapped from his home and has not been seen since.

Like thousands of other men, many of them students or workers in their early twenties, Ali volunteered to join the fight. His father gave him an old hunting rifle that he had kept hidden in the house for years. Others in Ali's unit joked that while Gaddafi's forces were pounding the city with anti-aircraft guns, Ali was fighting back with an anti-duck gun. "We were at the front line, but I never wanted to be right at the front. It was really scary, as we did not have a leader yet and the situation was very confused," Ali told me as we drove around the city one day. "I don't have a strong heart like some of the guys."

Nor was he sustained by faith. "You probably think that I am a Muslim, because of this," he said, pointing to a Quran on his dashboard.

“I did shout Allahu akbar when we fought, but I don't believe in God and that virgins for the martyrs stuff, and neither do many of my friends. We like to listen to music, get drunk on the beach on home-made alcohol. I just can't tell my family how I feel, because my uncle is the head of a mosque."

After a few days at the front, Ali's colleagues suggested he might be more useful working in the media centre. He agreed, and gave his hunting rifle to another member of his unit. Two days later, that man was shot in the stomach. “I never found out where my father's gun went," Ali said.

By the time I arrived in Misurata, the street battle had been raging for weeks. Most of Tripoli Street was controlled by snipers, but Ali agreed to drive me and two other journalists to the side roads that intersected it, where units of thowar were in combat with the snipers.

The car belonged to his brother and was a mess, cigarette boxes, shoes, biscuit wrappers and a few tins of sardines littering the floor. The boot was filled with tins of canned food. Ali slipped a CD titled Alternative Ballads into the car stereo: soft rock for a hard war.

We passed bakeries where men and boys were queuing for rations of bread. Despite the scarcity of goods, supermarkets had kept most of their prices stable. A shop manager told me: "This is a war, not a time to make money."

Cigarettes were the one exception. Rothmans, Ali's brand of choice, had quadrupled in price to ten dinars (about £4). There were thowar checkpoints every few hundred metres, reinforced with huge berms of sand brought from the beach, or large pieces of concrete pipe. At one roadblock, twisted remnants of missiles and shells fired by Gaddafi's soldiers into Misurata had been placed on top of one pipe. Next to it, with an arrow pointing towards the display, was a sign that read, "These are his weapons." Poking out of the pipe was a rake and spade: "These are our weapons."

Closer to the city centre, the tactics used by the thowar in the guerrilla war became evident. Giant shipping containers filled with wet sand and metal filings had been used to block off streets to prevent armoured columns getting through. Petrol-soaked blankets lay on the road, thrown there in the hope they would get caught in the tanks' tracks, allowing a Molotov cocktail or rocket-propelled grenade to set one of them on fire.

Leaving the car, we walked carefully down a side road to the main street, where several destroyed tanks hinted that the strategy had been successful. There had been an almighty battle; all the buildings were pockmarked by bullets. In places whole walls had been blown away. Splinters of glass and chunks of metal littered the street. A mosque had sustained heavy damage. There were burnt-out cars everywhere.

Closer to Tripoli Street, the damage had extended to residential homes, long abandoned by their occupants. Some of the side streets were within sight of the snipers, so Ali drove along new roads that been created by the thowar by punching holes in garden walls.

We were now very close to the vegetable market, where Gaddafi's troops had a base, protected by seven tanks. A group of about 20 fighters was having a breakfast of tuna and bread. They had been slowly clearing Gaddafi soldiers out of the neighbourhood, fighting house-to-house battles.

The leader of the unit was the only one wearing a uniform, which he'd taken from a captured Gaddafi soldier. He was a cartoonist's image of a rebel fighter - muscular, with a trim beard, a knife tucked into his belt at the back. Most thowar commanders had nicknames, but he was a replacement and new to the job; the previous leader had been killed by a sniper the day before. Ali suggested that we call him Mr Smile. He liked it.

He had been working in construction in Malta before the revolution, but had quit his job and taken a boat to Benghazi, where he received three weeks' basic training in light weapons. Now, as the leader of his group in Misurata, Mr Smile had control of a battle wagon that looked like something out of the Mad Max movies. A heavy machine-gun had been fixed on the back of a pick-up. Two giant rectangles of 12-millimetre-thick steel had been welded on to the front and rear of the vehicle. Mr Smile walked quickly towards Tripoli Street, waving his arm for us to join him. Coming to a crossing, he lowered his head and charged across.

“Snipers," he said. With gunfire zipping nearby, we bid Mr Smile goodbye. "Please come back and visit tomorrow," he said.

In this city, the abnormal quickly became normal. After a few nights sleeping on the AstroTurf floor of a basement gymnasium where journalists were put up, I no longer jumped at the rat-a-tat of gunfire, or the explosions or the ambulance sirens that pierced the night. Ordinary people in Misurata, who in January could barely tell the difference between a gunshot and a car backfiring, were - in their own minds at least - aural experts on heavy weapons.

Boom. "That's a Grad." Bang. "That's a mortar." Boom. "A tank shell."

Bang. "Katyusha rocket." Boom. "Nato must be bombing again."

War became normal for children, too. The schools were all closed, and for a while parents kept their children inside, but after a few weeks they were let out again to play. Ali's ten-year-old cousin started a game with his friends where they tried to find a full set of bullet shells, from a 7.62mm AK-47 round to a 50-calibre heavy-machine-gun round. Inevitably, there were accidents. One afternoon, on a visit to a clinic on the western outskirts of Misurata, I saw a 14-year-old boy, Abdishakur. He was sitting in a wheelchair because of his osteoporosis. It looked like he had measles, but in fact his face had been blasted with tiny fragments of shrapnel. His 11-year-old brother, Ibrahim, had even more severe injuries, sustaining damage to both eyes. His father, a local imam, explained what had happened.

“The boys were looking after my sheep when Ibrahim found a bullet still in its shell," he said. "They did not realise it was dangerous. They took it home. Ibrahim was hitting it when it exploded."

Family life had acquired a strange new reality. Neighbourhoods close to Tripoli Street or in other areas controlled by Gaddafi forces quickly emptied out. Families moved in with relatives or friends. If they had nowhere to go,

a stranger might offer up his house, and move his own family in with somebody else. One evening, I visited the home of Mohamed Tag­ouri, a 50-year-old who owned two water tankers. It was a large, well-maintained house with four bedrooms, ideal for Tagouri, his wife and their five children. Now, there were 11 families living in the house, 62 people in all. Tagouri's sister and her three children, all under five, were among them. Her house, near Tripoli Street, was now "junk", Tagouri said. Her husband was dead, killed on the front line a few days earlier. "Every family in Misurata has lost a relative," Tagouri told me as he sat on the floor, drinking coffee. "But we cannot stop resisting. We have to finish the situation. We have no regrets."

Most days, the shelling was not too heavy and he would drive one of his tankers to the desalination plant near the port, which had become the city's main supply after Gaddafi had cut the water mains. He would then sell the water in town, or give it away if someone was low on cash. Many were, as no salaries were being paid and no banks were open, although neighbourhood committees were handing out small sums of money to all families. They were handing out food parcels, too, but Tagouri said they lacked a crucial item. “There is no macaroni in Misurata."

Despite the best efforts of Mr Smile's team and other bands of thowar, the snipers of Tripoli Street were still causing havoc. No target was off limits: not the mosques, which broadcast "Allahu akbar" over and over to give the thowar strength, and not ambulances. Children, too, were seen as fair game.

At the hospital, I saw a ten-year-old boy who had been shot in the head while stepping outside to play with his friends. Such was the fear of snipers that some people had been too terrified to risk fleeing the city centre when the snipers came in. These included 101 orphans housed close to Tripoli Street. After huddling together in the basement of their orphanage for weeks, they had nearly run out of food. The power and water had been cut.

Selima al-Teer was one of two social workers trapped with the children. "My colleague and I were so afraid of snipers, but we decided we had to run to try and find food," she told me. "We took a hammer, ran about 500 metres to a supply store, and broke the door down. We put food in a wheelbarrow and ran back to the orphanage."

They made the journey three times. “Each time we just said to each other: 'May God help us,' and then ran," she said. Eventually, with the help of the thowar, all the children escaped and found refuge in a Quranic school in a safer suburb of the city.

As the days passed, it was clear that the thowar were gaining the upper hand on the snipers. By blocking the streets, they had managed to cut Gaddafi's resupply lines and began clearing buildings along Tripoli Street one by one. To identify the snipers' hideouts, the revolutionaries crept along side roads and then held out small pieces of mirror to look up the street, examining the reflection for the tell-tale puff of smoke whenever a shot was fired. Then they attacked the buildings with their Kalashni­kovs, heavy machine-guns and RPGs. Finally, they sent fighters into the buildings. They worked through the floors, sometimes tossing burning tyres into rooms to smoke out the last of the snipers.

One night, at the media centre, Ali told me that the eight-storey insurance building, the tallest in Misurata, which stood at the very centre of the city at the top of Tripoli Street, had been declared clear. We drove there early the next morning. For the first time the extent of the war here became obvious. Many of the buildings near the insurance tower resembled those of Mogadishu, in Somalia, where bullets have flown freely for 20 years. There were four destroyed tanks. A handful of local people wandered around in a near daze, struggling to grasp what had happened.

With Ali leading the way, we entered the darkened reception area of the insurance building, picking our way up the rubble-strewn stairs. We soon came upon some mattresses where a few snipers had been sleeping, and empty tuna and tomato paste tins. Spent shells lay in heaps on the floor. There was graffiti on the wall, which Ali translated. “If we survive, we are warning you gays and dogs. We will not forgive anybody from Misurata. We will fuck your daughters and your wives."

On the roof of the building, snipers had been sleeping in the elevator maintenance room, mattresses packed tightly together. Outside, there were thousands of spent shells on the terrace, along with several cases that had held anti-tank missile launchers. The roof had a panoramic view of the city and of the epic destruction below. To be up here with a gun was to be a master of downtown Misurata. However, after weeks of starving the snipers of food and ammunition, and hitting the buildings with gunfire, the thowar had cleared all the snipers from Tripoli Street.

And yet the death toll mounted. One day, I saw Mr Smile at the hospital, looking harried. He had lost a few men, he told me. The war had entered a new phase as the revolutionaries tried to push Gaddafi's soldiers out of their bases in the vegetable market and other locations. On the back foot - Gaddafi's minions called it a strategic retreat - the loyalist forces had increased their long-distance shelling of the city.

“Do you think this is over?" asked Hassan Mohamed, a 51-year-old man who was showing me around a destroyed house where 16 of Gaddafi's troops had been killed. He had already lost several family members in the conflict. He expected to lose more. Then he answered his own question: "This is not over. Gaddafi will send more soldiers. He is much bigger than the devil himself."

As the thowar pushed forward, there were terrible battles on the southern and western outskirts of the city. The scale of the missile and mortar attacks by the pro-Gaddafi troops increased, loyalist shells often falling on civilian neighbourhoods, whether intentionally or not. On one night of particularly heavy bombardment, Ali frantically searched the internet for information on the best place to take shelter in a house when bombs were falling. He then took the microphone at Radio Free Libya, which had become the voice of Misurata's revolution, and told people what he had learned.

As the first uncensored medium in the city in 42 years, the station offered an insight into some of the challenges that Libya might face once Gaddafi was gone. People of Ali's father's generation had pushed for Radio Free Libya to adhere to conservative values, with a strong focus on religion. But Ali and his friends, of the generation that had started the revolution and was dying on the front lines, wanted something more progressive. "Look, us young guys don't just know about camels or how to fix a car," he told me. "We have the internet. We know about the world."

I saw Tahar Alkesa in the emergency tent two nights before I left Misurata on 3 May. His stubble had turned into a beard, with patches of grey. He looked even more fatigued than when we had first met. The casualties had not slowed - ten to 20 killed most days and dozens of others injured. We could hear the boom, boom, boom of Grad missiles being launched by Gaddafi's troops in the distance. As they were slowly being pushed back outside the city, government forces had trained much of their attention on the port, determined to cut off Misurata's lifeline. A few days earlier, a small naval team sent by Gaddafi had been intercepted as it laid floating sea-mines outside the harbour.

Inside the hospital, while trying to ascertain the day's casualty figures, I bumped into Suleiman Ibrahim, a prominent businessman in Misurata who had been helping out around the hospital for weeks with Haythem, his younger brother. Haythem had left for Malta that morning on a boat - one of the very few able to enter the harbour in days - to sort out business in China. The men's two younger brothers, twins in their early twenties, were both working for the hospital, one as an ambulance paramedic, the other as a doctor. "This war is disastrous. Misurata has paid a big, big price," Suleiman said.

He was desperate to get his parents out of the country, but his mother had refused to leave unless all her sons did, too. They would not.

I had heard the reason many times from different people: we win, or we die.

Xan Rice is a contributing writer of the New Statesman.