When you heard that a gunman had slaughtered scores of Norwegian teenagers on a holiday island earlier this summer, did you think that here was another symptom of our sick and violent world? So did I, until I read Steven Pinker's brilliant, mind-altering book about the decline of violence. Pinker does not deny that individual human beings are capable of the most appalling acts of savagery. But the test of our propensity for violence is how the rest of us respond. Once it would have been basic human instinct to react to violence on this scale with more violence. But where were the reprisals, the mob rampages, the demands for the torture and killing of the perpetrator? Instead, the Norwegian people responded with remarkable compassion and restraint: love-bombing instead of real bombing. What happened in Norway this summer showed just how peace-loving we have become.

Pinker thinks that most of what we believe about violence is wrong. To convince us he sets himself two tasks. First, to demonstrate that the past was a far nastier place than we might have imagined. Second, that the present is far nicer than we might have noticed. So to start with we get a litany of horrors from ancient and not-so-ancient history: a catalogue of the unspeakable things that human beings have traditionally been willing to do to each other. This is slightly overdone, since anyone who thinks that, say, medieval Europe was a friendly, peaceable place can't have thought about it very much. Still, it is hard not to be occasionally struck dumb by just how horrible people used to be. The image I can't get out of my head is of a hollow brass cow used for roasting people alive. Its mouth was left open so that their screams would sound like the cow was mooing, adding to the amusement of onlookers.

The real fascination of this book is how we got from being a species that enjoyed the spectacle of roasting each other alive to one that believes child-killers have the same rights as everyone else. As Pinker shows, it is both a long story and a relatively recent one. The first thing that had to happen was the move from a nomadic, hunter-gatherer existence (where your chances of meeting a violent end could be as high as 50:50) to settled communities. The trouble was that early governments showed themselves at least as capable of cruelty as anyone else: most of the truly horrific instruments of torture Pinker describes were designed and employed by servants of the state. As the 17th-century philosopher John Locke remarked of the escape from the state of nature to so-called civilisation: why run away from polecats only to be devoured by lions?

So the next thing that had to happen was the state had to be properly civilised. This took place over the course of what we have come to call the enlightenment, thanks in part to philosophers such as Locke. In both private and public life – covering everything from table manners to bills of rights – the means were found to restrain our worst instincts. Slowly, painfully, but ultimately successfully torture was outlawed, slavery was abolished, democracy became established and people discovered that they could rely on the state to protect them.

Yet the enlightenment has acquired something of a bad name. Why? The answer is simply put: the 20th century, surely the most appallingly violent of them all, scarred by total war, genocide and other mass killings on an almost unimaginable scale. All those table manners and bills of rights didn't prevent the Holocaust, did they? At the heart of this book is Pinker's careful, compelling account of why the 20th century does not invalidate his thesis that violence is in a long decline. He makes his case in three ways. First, with a multitude of tables and charts he shows that our view of the century is coloured by presentism: we think it's the worst simply because it's the most recent and we know more about it. If we had equivalent coverage of the whole of human history (how many books have been published about the second world war compared to, say, the Mongol conquests of the 13th century?) we would see that all of it has been scarred by mass slaughters, some of them proportionately even worse than the horrors of the past hundred years.

Second, Pinker argues that the violence of the 20th century is best understood as a series of random spasms rather than part of a trend. The two world wars were essentially freak events, driven by contingency and in some cases lunacy: a bit like the killings on Utøya magnified a millionfold. They do not reflect the default condition of mankind. The evidence for this is the third part of Pinker's case: look at what has happened since 1945, as the world has become immeasurably more peaceful on almost every count. Of course, there have been horrors (Mao, Pol Pot) but no one can doubt that the arrow has been pointing away from the violence of the first half of the 20th century, not back towards more of it.

Pinker calls the post-1945 period "the long peace". But the real surprise is what he calls "the short peace", which corresponds to the 20 years since the end of the cold war. I am one of those who like to believe that the idea of 1989 as some fundamental turning point in human history is absurd: the world is just as dangerous as it has always been. But Pinker shows that for most people in most ways it has become much less dangerous. There have not just been fewer wars, but in the wars there have been many fewer people have died. Terrorism is down, not up. All sorts of disadvantaged groups – women, children, ethnic minorities, even animals – are much less likely to be victims of violence across many parts of the world, and the trend is spreading. Part of the reason we fail to notice this picture is that it is so pervasive: we are more aware of violence simply because we have become so unused to it.

At the outset Pinker calls the story he has to tell "maybe the most important thing that has ever happened in human history". That depends. If you told a medieval peasant that all the horsemen of the apocalypse that blighted his (and even more so her) life would be vanquished by the 21st century – famine and disease as well as war and violence – it might be the first two that seemed the real miracles (as well as being responsible for saving many more lives). Some peasants (though here perhaps more the hims than the hers) might also feel a little ambivalent about the decline of violence. Human aggression, unlike famine and disease, is not just some capricious act of God. It is part of who we are. Giving it up might leave even a victimised peasant feeling a little diminished.

Pinker accepts we have not abolished violence in the way that we have abolished smallpox. In the final section of the book he moves from history to evolutionary psychology to show that human beings are always torn between their inner demons and their better angels. What decides us between them is not virtue or vice but strategic calculation. We resort to violence when violence seems the better bet. We resist it when it seems riskier than the alternative. That's why violence can be self-reinforcing – as in the tit-for-tat world of the hunter-gatherers – but it's also why peace can be self-reinforcing – as in the love-bomb world we inhabit now. Pinker is adamant that we should not be complacent about the decline of violence: the inner demons are still there. But neither should we be fatalistic: as things stand, our better angels are a truer reflection of who we are.

What might change that? As I was reading this book I was repeatedly reminded of two novels. One is Lord of the Flies, an earlier generation's definitive allegory of the violence lurking in us all. Pinker's book makes Golding's vision look dated: there is no state of nature bubbling away beneath the surface of civilised man, notwithstanding all the hysterical nonsense that has been uttered about the recent riots (which were, for riots, remarkably unviolent). The other novel is Cormac McCarthy's The Road, this generation's definitive allegory of how it could all go wrong. McCarthy pictures a world in which some random future spasm (perhaps an environmental catastrophe) leaves us all unhinged and lets the inner demons loose. Does our gradual move away from violence towards civility leave us better or worse equipped to deal with the next great calamity when it comes? No one can know, and Pinker does not pretend to provide an answer. But in the meantime, everyone should read this astonishing book.

David Runciman's Political Hypocrisy is published by Princeton.