Sir Anthony Eden once spoke the following words, in perfect French: "One who has not suffered the horrors of an occupying power has no right to judge a nation that has." He was referring to France under Nazi occupation.

Eden's sentiment was correct. And yet the question that has been nagging many people since the second world war is why the record of some nations appears to have been so much better than others. Why, for example, did more than 70% of Dutch Jews disappear into the death camps, while almost all the Jews in Denmark managed to get away safely?

The relative degree of antisemitism does not offer a conclusive answer. There was anti-Jewish prejudice in both countries, but not, as Isaiah Berlin once nicely put it, "more than necessary". There was no violence against Jews. A reasonably assimilated Jewish middle-class was well established in both nations. Impoverished Jews, many of them in Amsterdam, were less assimilated, but they were not threatened either, until the Germans came.

Were the Danes more courageous in opposing Nazi plans, or were they simply nicer people – like the Italians, who also managed to protect a large number of Jews from mass murder?

Ranking entire nations in terms of niceness is probably an idea to be resisted: societies are too diverse for that. But how do we explain the discrepancies, between Holland and Denmark, or Poland, where most Jews were murdered, and Bulgaria, where many survived?

Bo Lidegaard, in this magisterial study of wartime Denmark, claims that his country's admirable record owes much to the way Danish citizens saw themselves and their society. He writes: "The Danish exception shows that the mobilisation of civil society's humanism and protective engagement is not only a theoretical possibility: It can be done. We know because it happened." The harming of Denmark's Jews went against everything most Danes believed in, especially their concept of the rule of law. Even injustice, he writes, "needs a semblance of law. That is hard to find when the entire society denies the right of the stronger."

The story he tells of how Danes, from the top bureaucrats, Church leaders and police officials down to the humblest fishermen, helped the Jews escape when the Germans tried to deport them to concentration camps in October 1943, is indeed astonishing and heart-warming.

From the moment they knew it was coming, Danish government officials made it clear to their policemen that no help was to be given to the Germans. Doors were opened everywhere, often to complete strangers, for Jews to hide. Fishermen living along the rugged Danish coast loaded their cutters and schooners with thousands of refugees and ferried them across to Sweden. They were often generously paid for taking the risk. But penniless Jews, who had fled from eastern Europe or were members of the Danish working class, were never rejected.

And all this was openly supported by King Christian. He did not, contrary to popular myth, ride his horse through Copenhagen wearing the Star of David, but he did make it clear, as he wrote in his diary, that he considered "our own Jews to be Danish citizens, and the Germans could not touch them". The Dutch Queen Wilhelmina may have felt the same way about Jews in her country, but she never stated it as openly as her Danish colleague, even from her safe base in wartime London.

The humanism of the Danish rescuers, and their king, is not in doubt. Helping people in mortal danger was a matter of common decency, which was not always in such abundant supply in other parts of Europe. Again and again, Lidegaard turns to a political explanation: "Danish democracy mobilised itself to protect the values on which it was based."

To the fisherfolk, according to Lidegaard, it was also a matter of local honour. A school consultant told his fellow citizens in a coastal village from which many Jews managed to escape: "History will be written these days in this town."

However, there is no reason to think that the Dutch, or the Norwegians, or the Belgians, were any less attached to their democratic institutions or their idea of the rule of law. To observe, as Lidegaard does, that Denmark benefited from circumstances that were substantially different from those in other countries under Nazi occupation, does not detract from the moral courage of Danish citizens, but does help explain some of the discrepancies in Europe.

First, there were very few Jews in Denmark, about 6,000, compared to about 140,000 in Holland, and more than 3 million in Poland. Most of them were so assimilated as to be indistinguishable from other Danes. Also, it was relatively easy to escape to neighbouring Sweden, which was not under German occupation. And neutral Sweden, realising by 1943 that the Third Reich was in retreat, welcomed the Jewish refugees. This act of humanity was boosted, perhaps, by a desire to show that Sweden would be on the winning side.

The most important factor, however, was the highly ambivalent attitude of the Germans themselves. Unlike Sweden, Denmark was not free from Nazi occupation, but a special deal had been struck. Hitler wanted Denmark to be his model protectorate. Like the Swedes, the Danes would supply the Reich with agricultural goods and other economic assistance. They would crack down on domestic resistance. And in return, they retained their own government, as well as their cherished democratic institutions.

Holland, by contrast, was under direct Nazi rule; its government had fled to London in 1940, along with the Queen. Much of France was still administered by a French government, which was neither liberal, nor democratic. Central European countries were either annexed by the Reich or ruled by Nazi governors.

Swedish neutrality and the Danish deal with Nazi Germany were not heroic. But these arrangements provided the necessary flexibility to do the right thing. In a way, it was precisely their unheroic accommodation that allowed them to behave decently. As long as the Germans remained keen to keep Denmark as a somewhat autonomous protectorate, it was not in their interest to agitate the Danes. And the Danes made it very plain that any attempt to deport the Jews would agitate them very much.

It also helped that the decision to attack the Jews in Denmark came late – in 1943 – and suddenly. There was no gradual isolation of the Jewish population through cumulative measures, which often met with relatively little protest in other occupied countries: the barring from certain jobs or public places, the yellow star, and so on.

Also, by 1943, even some of the fiercest Nazi officials were becoming a little nervous about the possible consequences of what they were doing in case the Reich should collapse. This would explain the two-faced and oddly equivocal actions of the top Nazi official in Denmark, SS General Werner Best, and of his chief German adviser, Georg Duckwitz, who told the Danes exactly when and where the attack on the Jews would take place.

At that stage, relations between Nazi Germany and its model protectorate were in a delicate phase anyway, because the elected Danish government had already resigned in protest against the imposition of martial law. If the Danish bureaucrats who continued to administer the country were to quit as well, the German-Danish deal would break down, which would not only mean a loss of face in Berlin, but also hinder the steady supply of Danish goods to the Reich.

That is why Werner Best assured his boss, Heinrich Himmler, that he would take care of the Jewish problem in Denmark, while at the same time hinting to the Danes that he would limit the scope of the deportations. He may even have been complicit in Duckwitz's tip-off about the impending action, which allowed most Jews to go into hiding. What is sure is that neither the German army, nor the navy, nor even Nazi officials in Denmark, apart from a few zealots, did very much to stop the boats from ferrying their loads of refugees to Swedish ports.

Lidegaard is right: "The special Danish example cannot be used to reproach others who experienced the German occupation under far worse conditions than Denmark." But the Danes made the very best out of their easier circumstances. And for that, they should always be remembered as an example of civility at a time when there was precious little of it.

• Ian Buruma's most recent book is Year Zero: A History of 1945.