Outside, snow blankets the steep rooftops and ice seals the pond. Inside, by the red glow of the hearth, faces flushed with beer debate the meaning of the scriptures and the rights of the king. And then horses' hooves are heard thundering into the frozen village square, above the yells of soldiers.

That's how I picture the moment before Pieter Bruegel's painting The Massacre of the Innocents. In Bruegel's snowscape, a village is being turned upside down by soldiers who have come on behalf of Philip II, king of Spain, to punish heresy, to show what happens when provinces revolt. The armoured horsemen are a tight, compressed mass, black against white snow; they wait in patient menace while footsoldiers with pikes and battering rams bash in the doors of houses and drag villagers into the main square. They are violently investigating souls. Women sit weeping in the snow. A man is on his knees, begging mercy from the finely clothed officers on their horses, who seem amused by it all.

At some point in the past, the infants in this painting in the Royal Collection, on view at Hampton Court, being thrown about, slaughtered, mourned by screaming mothers, were replaced by packages and animals - a transformation that makes the hidden meaning of the picture manifest. The owner, who was so shocked by Bruegel's realistic violence that he asked for the "innocents" to be concealed, accidentally exposed what this painting is really about: the sack of a village during Spain's violent repression of Protestant Netherlandish rebellion in the late 16th century.

Bruegel's Massacre is a picture whose authority everyone recognises, a chilling complement to his lovely winter landscape The Hunters in the Snow. The homeliness of the hunters' world is overturned in the Massacre by death battering at the door. Death was doing this regularly in Bruegel's southern Netherlands in the late 1560s, as the beginnings of the Dutch revolt against Catholic rule provoked vicious repression. In 1565, despite the urging of local nobles for moderation, Philip II reaffirmed the death penalty for heresy among his Netherlands subjects; in 1566 there were Calvinist riots; in 1567 the Duke of Alba was sent with an army to try to crush dissent for good, resulting in one of the cruellest military campaigns in European history.

In the Prado in Madrid, you can compare Bruegel's works with the paintings that influenced him and shaped his idea of what art might be: the delirious phantasms of Hieronymous Bosch. It's a train of thought also pursued by the National Gallery's new show, Bosch and Bruegel: Inventions, Enigmas and Variations.

Bruegel was obsessed with Bosch, painter of The Garden of Earthly Delights and the genius of Flemish Renaissance art. Bosch was hugely popular; Bruegel was the only artist who could recapture his folly, delusion and delight - Bosch's painterly heir. But he made some alterations to the lunatic tower he inherited.

Hieronymous Bosch lived in 's-Hertogenbosch, near Antwerp; he was born in about 1450 and died in 1516. He was far and away the most brilliant genius of northern Renaissance art, a mind of incomparable imaginative freedom. Bosch's paintings are full of giant birds, buildings like kidneys and livers with saws stuck through them, severed ears, bubble-houses, giraffes, Jesus and all the rest. He paints an unreality dredged from the depths of the late medieval mind. He imagines underworlds; he also invents utopias. Thomas More, his contemporary, claimed it was in Antwerp that he met the sailor who told him about Utopia, the account of the far-off island society he wrote in 1516, the year Bosch died.

Bosch's countryman Erasmus of Rotterdam, the intellectual leader of the northern Renaissance, argued in his book Praise of Folly in 1509 that foolishness, ugliness, hilarity have a place in life, perhaps even in religion. Thinking of that kind, as well as the magnetism of his images (which has never faded), helps to explain why Bosch became so sought after in 16th-century Europe. If he fascinated Bruegel, he also preoccupied Philip II of Spain, architect of the Netherlands' misery, who owned Bosch's Garden of Earthly Delights and the Haywain, and who contemplated their conundrums in the shadows of his monastic palace, the Escorial.

Bruegel was born in 1525 and worked in Antwerp and Brussels, dying in 1569. His career was founded on wild, symbolic, dreamlike images that reminded people of Bosch. And yet it is the ways in which he departs from Bosch that make him great.

Bosch is the prisoner of his fantasies. There is a traumatic painting by him in the Prado of the Temptations of St Anthony; you can't help seeing this image of a mind beset by freakishness and chaos as autobiographical. The same sense of being surrounded by demons makes Bosch's Christ Mocked, in the National Gallery, quite terrifying. This group, with a human Christ surrounded by inhuman grotesques, closely resembles a famous drawing by Leonardo da Vinci (coincidentally - they never met) of a wise man fallen among gross idiots that can be interpreted as a melancholy self-portrait. Maybe Bosch's painting is also a self-portrait, of a man tortured by hideous phantoms.

Bruegel is more down-to-earth - and far less personal. He was more in the world than his predecessor. Bruegel travelled to Italy, and intimations of Italian art and architecture crop up in his paintings in the least likely places. His Tower of Babel, a prodigious termite city crumbling upwards, has enormous internal buttresses based on the ones he saw in the shell of the Colosseum in Rome.

He lived in interesting times. Outside the Netherlands, the brutality of those days is forgotten; yet Bruegel's images of war, from Dulle Griet (which inspired Brecht's Mother Courage) to The Suicide of Saul (whose swarming army returns in Peter Jackson's Lord of the Rings films) live on. When we see Bruegel's Massacre of the Innocents, we know we are glimpsing something with emotional and reporterly truth, even though the red faces of the soldiers are bulbous caricatures and the stocky bodies of the peasant women are grey cartoons against the snow. Bruegel's allure lies exactly in this tension between fantasy and history. The reason his works are unforgettable is that, in their wild fantastic excess, they strike us as real. Especially when he pictures the end of the world.

In about 1560, Bruegel painted a scene from the ultimate war. The Triumph of Death is unmistakably a war painting, a history painting. In a panoramic landscape, Death's army rages, innumerable, invincible. Thousands of skeletons mass behind coffin-shields under Death's banners. As a skeleton beats the war drums, they deploy new technologies including a giant box raised by a lever into which men and women are savagely shoved. Some humans fight back, but these isolated acts of courage have no significance.

Another army of death approaches in a bony column; people are executed on the hilltops by skeletons swinging mighty swords, their bodies displayed on circular raised platforms. There are ships burning in the distance, a red glow of horror beyond the horizon: evidently, the same terrible scenes are taking place everywhere in the world.

It's impossible to miss the taste of history in this picture. The unstoppable army of skeletons has something acutely satirical about it; this is what war is like. In its very fantasy and excess, Bruegel's painting reveals truths never officially acknowledged about the way the world is.

Bruegel is a realist - even a social realist. This is why his art appealed to 20th-century socialist and communist writers such as WH Auden and Bertolt Brecht. He takes the mad, foolish, impossible fantastic realm mapped by Bosch and demonstrates, with brutal peasant humour, that it is not so far after all from the everyday cruelties and injustices and stupidities we accept as natural. Bosch was not mad, Bruegel says - he was a prophet. You do not need to close your eyes to see monstrosities. They are all around us.

In 1557 Bruegel engraved a print called Big Fish Eat Little Ones. It was successfully marketed as a work by Bosch, but it's more pointed than anything Bosch would have done. The spectacle of a giant fish dragged onto the shore, spewing dozens of smaller fish out of its gawping mouth while men slice open its belly to reveal even more fishlings, has all the power of Bosch - but is far easier to read. Bosch's paintings have never been convincingly deciphered. Bruegel's print is immediately understandable: it's a savage moral satire, a bleak economic truth. Big fish eat little ones, the rich eat the poor. The parable still works. I have a paperback about global capitalism somewhere with Bruegel's image on the cover.

Even in their different versions of The Adoration of the Magi, at the heart of the National Gallery's exhibition, the contrast between Bosch and his follower is telling. Bosch's Adoration of the Magi (the National is exhibiting a studio copy of the Prado original) has a stunning background of science-fiction architecture: the round skyscrapers, tubular towers and pyramid-spires of Bosch's Bethlehem look like they came off the cover of a 1970sAsimov paperback - or failing that, out of Stalin's Moscow. These buildings have no precedent or classical model; they are glimpses of utopia that make it hard to pay attention to the miraculous birth in the foreground.

Bruegel, however, shifts our attention precisely to the tough realities of the stable scene. The withered flesh of the Magi makes them an unimpressive bunch, and they have come to see the infant Christ accompanied by a large armed guard. Soldiers press towards the stable, their vicious-bladed pikes closing the sky, a military presence as jarring and sinister as the death squad in The Massacre of the Innocents.

Money, war, religion - the insanities that disorder Bruegel's world are all too recognisable. Bosch is an architect of the unreal, Bruegel a historian of the horrors we know.

· Bosch and Bruegel is at the National Gallery, London WC2, from January 24 until April 4. Details: 020-7747 2885.