The 65th anniversary of the D-day landings this week is an occasion to revisit joyful pictures of the liberation of France in 1944. But among the cheering images there are also shocking ones. These show the fate of women accused of "collaboration horizontale". It is impossible to forget Robert Capa's fallen-Madonna image of a shaven-headed young woman, cradling her baby, implicitly the result of a relationship with a German soldier.

The punishment of shaving a woman's head had biblical origins. In Europe, the practice dated back to the dark ages, with the Visigoths. During the middle ages, this mark of shame, denuding a woman of what was supposed to be her most seductive feature, was commonly a punishment for adultery. Shaving women's heads as a mark of retribution and humiliation was reintroduced in the 20th century. After French troops occupied the Rhineland in 1923, German women who had relations with them later suffered the same fate. And during the second world war, the Nazi state issued orders that German women accused of sleeping with non-Aryans or foreign prisoners employed on farms should also be publicly punished in this way.

Also during the Spanish civil war, Falangists had shaved the heads of women from republican families, treating them as if they were prostitutes. Those on the extreme right had convinced themselves that the left believed in free love. (The most famous victim in fiction is Maria, the lover of Robert Jordan in Hemingway's For Whom the Bell Tolls.)

It may seem strange that head-shaving, essentially a rightwing phenomenon, should have become so widespread during the leftist liberation euphoria in France in 1944. But many of the tondeurs, the head-shavers, were not members of the resistance. Quite a few had been petty collaborators themselves, and sought to divert attention from their own lack of resistance credentials. Yet resistance groups could also be merciless towards women. In Brittany it is said that a third of those civilians killed in reprisals were women. And threats of head-shaving had been made in the resistance underground press since 1941.

There was a strong element of vicarious eroticism among the tondeurs and their crowd, even though the punishment they were about to inflict symbolised the desexualisation of their victim. This "ugly carnival" became the pattern soon after D-day. Once a city, town or village had been liberated by the allies or the resistance, the shearers would get to work. In mid-June, on the market day following the capture of the town of Carentan, a dozen women were shorn publicly. In Cherbourg on 14 July, a truckload of young women, most of them teenagers, were driven through the streets. In Villedieu, one of the victims was a woman who had simply been a cleaner in the local German military headquarters.

Many French people as well as allied troops were sickened by the treatment meted out to these women accused of collaboration horizontale with German soldiers. A large number of the victims were prostitutes who had simply plied their trade with Germans as well as Frenchmen, although in some areas it was accepted that their conduct was professional rather than political. Others were silly teenagers who had associated with German soldiers out of bravado or boredom. In a number of cases, female schoolteachers who, living alone, had German soldiers billeted on them, were falsely denounced for having been a "mattress for the boches". Women accused of having had an abortion were also assumed to have consorted with Germans.

Many victims were young mothers, whose husbands were in German prisoner-of-war camps. During the war, they often had no means of support, and their only hope of obtaining food for themselves and their children was to accept a liaison with a German soldier. As the German writer Ernst Jünger observed from the luxury of the Tour d'Argent restaurant in Paris, "food is power".

Jealousy masqueraded as moral outrage, because people envied the food and entertainment these women had received as a result of their conduct. When Arletty, the great actor and star of the film Les Enfants du Paradis, died in 1992, she received admiring obituaries that did not mention the rumour that she had her head shaved at the liberation. These obituaries even passed over her controversial love affair with a Luftwaffe officer. But letters to some newspapers revealed a lingering bitterness nearly 50 years later. It was not the fact that Arletty had slept with the enemy which angered them, but the way she had eaten well in the Hôtel Ritz while the rest of France was hungry.

After the humiliation of a public head-shaving, the tondues - the shorn women - were often paraded through the streets on the back of a lorry, occasionally to the sound of a drum as if it were a tumbril and France was reliving the revolution of 1789. Some were daubed with tar, some stripped half naked, some marked with swastikas in paint or lipstick. In Bayeux, Churchill's private secretary Jock Colville recorded his reactions to one such scene. "I watched an open lorry drive past, to the accompaniment of boos and catcalls from the French populace, with a dozen miserable women in the back, every hair on their heads shaved off. They were in tears, hanging their heads in shame. While disgusted by this cruelty, I reflected that we British had known no invasion or occupation for some 900 years. So we were not the best judges."

The American historian Forrest Pogue wrote of the victims that "their look, in the hands of their tormentors, was that of a hunted animal". Colonel Harry D McHugh, the commander of an American infantry regiment near Argentan, reported: "The French were rounding up collaborators, cutting their hair off and burning it in huge piles, which one could smell miles away. Also, women collaborators were forced to run the gauntlet and were really beaten."

Elsewhere some men who had volunteered to work in German factories had their heads shaved, but that was an exception. Women almost always were the first targets, because they offered the easiest and most vulnerable scapegoats, particularly for those men who had joined the resistance at the last moment. Altogether, at least 20,000 women are known to have had their heads shaved. But the true figure may well be higher, considering that some estimates put the number of French children fathered by members of the Wehrmacht as high as 80,000.

In Paris there were cases of prostitutes kicked to death for having accepted German soldiers as clients. And at the other end of the social scale, several women from the highest reaches of the aristocracy were sheared for consorting with German officers. But resistance leaders in Paris made a determined effort to stop all head-shaving. Colonel Henri Rol-Tanguy had posters run off warning of reprisals against would-be tondeurs, and René Porte, another leader who was renowned for his strength, knocked together the heads of a group of youths tormenting a young woman.

While many allied troops were sympathetic to France's suffering under the occupation, a considerable number had their worst prejudices confirmed by what they saw. American troops who had never been abroad before tended to see France itself as an enemy country, despite the attempts of the military authorities to inform them of the true situation. Some officers gave orders to arrest or shoot any French civilians encountered in the immediate invasion areas. Certainly French men and women found with German weapons were shot on the spot before they had a chance to explain. The possibility that they might have been collecting these weapons for the resistance never occurred to the soldiers concerned.

An extraordinary battlefield myth soon spread like wildfire. This maintained that young French women, the lovers of German soldiers, were fighting as snipers against the allies. These rumours were soon picked by British and American war correspondents eager for sensational stories. But a number of incidents also found their way into official reports without any doubts expressed about their authenticity. For example a lieutenant with the American 1st Infantry Division reported that they had encountered "four women in German uniform as snipers in trees and five in the town. I only saw one closely enough to identify her as a woman. She wore the German uniform and looked like a French woman."

Churchill heard these stories of women snipers during his visit to Normandy on 12 June and wrote about them to Anthony Eden on his return. British officers, however, later became increasingly sceptical of these "latrine rumours".

Moral confusion, if not outright hypocrisy, existed on the allied side too. At his airfield near Bayeux, Colville found it ironic when General Montgomery ordered all brothels to be closed. "Military police were posted to ensure that the order was obeyed. Undeterred and unabashed, several of the deprived ladies presented themselves in a field adjoining our orchard. Lines of airmen, including, I regret to say, the worthy Roman Catholic French-Canadians, queued for their services, clutching such articles as tins of sardines for payment."

The French, meanwhile, were shocked by the attitude of some American soldiers, who seemed to think that when it came to young French women "everything can be bought". After an evening's drinking, they would knock on farmhouse doors asking if there was a "mademoiselle" for them. Supposedly useful gambits were also provided in daily French lessons published by the US Armed Forces publication Stars and Stripes, including the phrase for "My wife doesn't understand me."

Americans and British saw liberated Paris not just as a symbol of Europe's freedom from Nazi oppression, but as a playground for their amusement. "As we neared the city we were seized by a wild sort of excitement," wrote Pogue. "We began to giggle, to sing, yell and otherwise show exuberance." But when Pogue reached Paris, he was shaken to find that American military authorities had taken over the Petit Palais and erected a large sign announcing the distribution of free condoms to US troops. In Pigalle, rapidly dubbed "Pig Alley" by GIs, French prostitutes were coping with more than 10,000 men a day. The French were also deeply shocked to see US soldiers lying drunk on the pavements of the Place Vendôme. The contrast with off-duty German troops, who had been forbidden even to smoke in the street, could hardly have been greater.

The basically misogynistic reaction of head-shaving during the liberation of France was repeated in Belgium, Italy and Norway and, to a lesser extent, in the Netherlands. In France, another wave of head-shaving took place in the late spring of 1945 when forced labourers, prisoners of war and concentration camp victims returned from Germany. Revenge on women represented a form of expiation for the frustrations and sense of impotence among males humiliated by their country's occupation. One could almost say that it was the equivalent of rape by the victor.

• Antony Beevor's D-day - The Battle for Normandy is published by Viking Penguin. To order a copy for £23 with free UK p&p go to theguardian.com/bookshop or call 0330 333 6846