"This is a bad time to be a black man in Libya," reported Alex Thomson on Channel 4 News on Sunday. Elsewhere, Kim Sengupta reported for the Independent on the 30 bodies lying decomposing in Tripoli. The majority of them, allegedly mercenaries for Muammar Gaddafi, were black. They had been killed at a makeshift hospital, some on stretchers, some in an ambulance. "Libyan people don't like people with dark skins," a militiaman explained in reference to the arrests of black men.

The basis of this is rumours, disseminated early in the rebellion, of African mercenaries being unleashed on the opposition. Amnesty International's Donatella Rivera was among researchers who examined this allegation and found no evidence for it. Peter Bouckaert of Human Rights Watch similarly had not "identified one mercenary" among the scores of men being arrested and falsely labelled by journalists as such.

Lurking behind this is racism. Libya is an African nation – however, the term "Africans" is used in Libya to reference the country's black minority. The Amnesty International researcher Diana Eltahawy says that the rebels taking control of Libya have tapped into "existing xenophobia". The New York Times refers to "racist overtones", but sometimes the racism is explicit. A rebel slogan painted in Misrata during the fighting salutes "the brigade for purging slaves, black skin". A consequence of this racism has been mass arrests of black men, and gruesome killings – just some of the various atrocities that human rights organisations blame rebels for. The racialisation of this conflict does not end with hatred of "Africans". Graffiti by rebels frequently depicted Gaddafi as a demonic Jew.

How did it come to this? A spectacular revolution, speaking the language of democracy and showing tremendous courage in the face of brutal repression, has been disgraced. Racism did not begin with the rebellion – Gaddafi's regime exploited 2 million migrant workers while discriminating against them – but it has suffused the rebels' hatred of the violently authoritarian regime they have just replaced.

An explanation for this can be found in the weaknesses of the revolt itself. The upsurge beginning on 17 February hinged on an alliance between middle class human rights activists and the working classes in eastern cities such as Benghazi. Rather than wilting under repression, the rebellion spread to new towns and cities. Elements of the regime, seeing the writing on the wall, began to defect. Military leaders, politicians and sections of business and academia sided with the rebels.

But the trouble was that the movement was almost emerging from nowhere. Unlike in Egypt, where a decade of activism and labour insurgency had cultivated networks of activists and trade unionists capable of outfoxing the dictatorship, Libya was not permitted a minimal space for civil society opposition. As a result, there was no institutional structure able to express this movement, no independent trade union movement, and certainly little in the way of an organised left. Into this space stepped those who had the greatest resources – the former regime notables, businessmen and professionals, as well as exiles. It was they who formed the National Transitional Council (NTC).

The dominance of relatively conservative elites and the absence of countervailing pressures skewed the politics of the rebellion. We hear of "the masses", and "solidarity". But masses can be addressed on many grounds – some reactionary. There are also many bases for solidarity – some exclusionary. The scapegoating of black workers makes sense from the perspective of elites. For them, Libya was not a society divided on class lines from which many of them had profited. It was united against a usurper inhabiting an alien compound and surviving through foreign power. Instead, the more success Gaddafi had in stabilising his regime, the more the explanation for this relied on the claim that "Gaddafi is killing us with his Africans ".

A further, unavoidable twist is the alliance with Nato. The February revolt involved hundreds of thousands of people across Libya. By early March the movement was in retreat, overseas special forces were entering Libya, and senior figures in the rebellion called for external intervention. Initially isolated, they gained credibility as Gaddafi gained ground. As a result, the initiative passed from a very large popular base to a relatively small number of armed fighters under the direction of the NTC and Nato. It was the rebel army that subsequently took the lead in persecuting black workers.

Under different conditions, perhaps, unity between the oppressed was possible. But this would probably have required a more radical alliance, one as potentially perilous for those now grooming themselves for office as for Gaddafi. As it is, the success of the rebels contains a tragic defeat. The original emancipatory impulse of February 17 lies, for now, among the corpses of "Africans" in Tripoli.