The thundering transport plane heaves itself into the sky over Tripoli, engines screaming, and from above you finally see the havoc Libya's militias have wrought in their own capital in the last few weeks. Eruptions belch from impacts of Grad rockets and tank shells across swaths of this dun-coloured city. Black smoke curls from burning oil tanks, white from house-fires. In among it all, cars as small as toys speed away, packed with families searching for elusive safety.

Three years ago such bombardment brought Nato jets screaming from the skies to bomb Muammar Gaddafi's forces and deliver victory to the rebels. Now those same rebels are causing carnage, but the only Nato planes are deployed, like this one, for evacuation.

After weeks of fighting, the outside world has thrown in the towel on Libya. Foreigners, and a few lucky Libyans, are streaming away by any means possible. Some by road, through the gauntlet of militia checkpoints to Tunisia, some plucked from the port by warship, some jammed in the belly of a hulking Italian air force C-130 Hercules. The Americans went first, a great convoy of armoured Jeeps snaking out from their fortified embassy under air cover. That was the signal for a mass exodus. The British sent non-essential staff on the same route as the Americans but, lacking air cover, saw their Jeeps raked with gunfire and forced back.

The C-130 left last week from Mitiga, the smaller of Tripoli's two airports, because the larger one is in flames, and the lucky evacuees scrambled aboard through the big open tailgate, past grinning Italian special forces soldiers in desert combat gear and designer sunglasses.

Once on board, jammed into canvas seats, nobody spoke; everyone was listening to the detonations still audible outside. Those detonations had been daytime companions all week, sounding like someone banging a massive distant drum, forever searching for a rhythm, forever failing to find one.

The big cargo door whined shut but there was a problem; the tractor towing the generator that fed power to the plane had broken down and the generator was too near the propellers for the plane to move. Attempts to fix the tractor failed so, finally, Libyan volunteers assembled to heave it away. Passengers looked out through grimy portholes, and the volunteers looked back, both parties knowing on which side of those windows it was better to be. And then we were off.

Propellers churned, engines roared and the boxy plane rattled and bumped down the runway, our only concern the chance of a missile or bullet singing in from the perimeter. Opposite me, an air crewman tightened the straps of his heavy green flak jacket. I didn't have one – David Cameron had assured everyone three years ago that they would not be needed, after he arrived in Tripoli to take credit for Nato's intervention and famously said that Libya's revolution would "give an example to others about what taking back your country can mean".

The plane zipped off the tarmac in the steepest of climbs, passengers clinging to their harnesses, everyone trying to guess how high you had to get to be safe from ground fire.

How Libya's freedom came undone could fill volumes. The short version is that the first democratically elected parliament, the General National Congress, rather than disband the militias, funded them, each faction seeing its own forces as insurance against those of everyone else.

An Islamist-led coalition came to dominate parliament, but as the squabbling grew worse it realised it would lose an election, so delayed having one. Then, in May, a former Gaddafi-era general turned rebel leader, Khalifa Hiftar, launched an offensive against Islamist brigades in the east while his allies stormed congress in Tripoli. An election was duly called in June, and the Islamists duly lost, or expect to lose when parliament assembles this week. The result has seen some of their militias grab what Tripoli real estate they can, triggering civil war.

The west will get the blame for this spectacular unravelling – and deserves some of it. Diplomats prodded Libya's new government to be transparent, while encouraging US, French and Italian defence companies to be anything but transparent in selling weapons to a country already awash with them. But Libya's freedom was in the end Libya's problem to solve. The fact that it failed is related to the atomised society left behind by 40 years of the most brutal and erratic of dictatorships. Libyans have a saying: "Within Libya it is region against region, within regions, tribe against tribe, within tribes, family against family."

The militias, for all their supposed differences, are remarkably similar. Each features a larger-than-life warlord and ragtag followers kitted out in mix'n'match uniforms. Their weapon of choice is a battered pickup truck fitted with an anti-aircraft cannon – like a machine gun, but bigger, and firing shells, not bullets – bolted to the flat bed. Their training is rudimentary, their firing wild, their battles in built-up areas deadly for local civilians.

The Islamists call themselves revolutionaries, implying that anyone opposed to them is against the revolution. Their opponents also call themselves revolutionaries, labelling the Islamists "terrorists", while the Islamists accuse their opponents of following Gaddafi. Neither label is true: both sides have plenty to give that is positive. But the time for giving in Libya seems past.

A few nights before the evacuation, I drank hot chocolate topped with cream with a Libyan photographer friend at a city-centre cafe nicknamed The Clock after a nearby handsome clock tower, presented to the city long ago by an Ottoman pasha. Hours later, three of his friends were arrested by one of the new amateur secret police teams, later freed unharmed, but for the moment he was relaxed and contemplative.

"We are like a class of kids where the bad teacher is suddenly dead," he said. "Now we all fight each other."

It was as good an explanation as any for a country stuck so long in the dark that when the light finally came, it was blinding.

"You know my problem?" asked the photographer, who will not use his name in print, fearing his history as a rebel fighter will count against him when he tries to get a European visa. "My problem is, it's hard to be a radical moderate."

The transport plane claws its way up into welcoming fluffy cloud, popping out the other side over the big blue sea. Safe from missile fire, relief spreads through the aircraft. The crewman opposite shrugs off his flak jacket, passengers puff out their cheeks or close their eyes. A tall young woman in a red T-shirt stares into space. Anxiety is followed by relief, relief by guilt at those left behind, and guilt from memories of three years spent watching Libya's freedom unravel.

There is the memory of a cheerful militiaman, the spitting image of Robert Downey Jr, deployed at Tripoli zoo and making it his business to feed the animals, including Gaddafi's white lion, telling me: "If you'd said a year ago I'd be making sure the hippopotamus had the right food mix ..."; of the Naked Lady, an Italian colonial-era bronze statue of a bare-breasted woman in downtown Tripoli, beloved by all but the militants, who put an axe through her face; and of the Benghazi militiaman, texted by his commander about rioters attacking their base, who chose instead to continue his game of five-a-side football.

And there is the saddest story I ever heard, from one of the bravest men I ever met: Mohamed – oil worker, husband, father – who picked up a gun in the revolution, fighting in the hardest battles with the blood of his slain best friend, killed alongside him, splashed on his boots. When the guns fell silent he visited wounded comrades in hospital, most hardly out of their teens, missing hands and legs and arms and feet. By day they joked and raced wheelchairs but in the evening, he told me, they began to cry.

There is also the cherished memory of standing on the balcony at sunset as the call to prayer rose from Tripoli's minarets in a great surging symphony. Nowhere does sunsets like Libya, a golden seraphic light falling on the palms and beaches and sparkling sea, bringing pause even to the fighters.

Flying away, I leave the country as I found it, back at war. It is a country so rich in possibility and so undone by a chaos you can unpick for ever without getting to the nub.

My photographer friend had the answer. "Confused?" he told me. "Then you understand Libya."