In the late summer of 1842, two British armies marched through Afghanistan to avenge the destruction of a force sent three years earlier to effect a regime change in the central Asian kingdom. That mission – one of the largest military expeditions mounted by British troops and their local reinforcements in decades — had gone disastrously wrong. Much of the force had been wiped out as it attempted an ignominious retreat from Kabul, leaving a weak and disillusioned ruler on the throne, and the reputation of the most fearsome military machine in the world badly tarnished. The task of the two armies sent back to the Afghan capital thus had a simple task: retribution.

The long columns of troops and camp followers marched through the debris of the earlier massacres. Those coming in from the east swiftly found the remains of the men of the 44th Foot on the small hill where they had made a last stand. Among the skeletons, those of "the officers were plainly distinguishable by the long hair which still remained attached to their skulls", remembered Thomas Seaton, a young British infantryman. One narrow defile had to be cleared of hundreds of corpses still "impaled on a barrier of thorn bushes" built by the Afghan tribesmen to block the route of retreating troops the previous winter. Vultures circled overhead, picking desiccated flesh off the deep-frozen and now thawed skeletons, "thrown into heaps of eight to a hundred", of sepoy soldiers from India's northern plains who had died in the snows of the Afghan winter, cut up or simply stripped where they lay, wounded, by their enemies. Elsewhere, in a small watchtower, Seaton found rooms "filled with decaying bodies, up to the very roof, and there was a mound of them outside … completely covering the steps".

As it marched, the "army of retribution" levelled villages, burned crops, cut down precious trees, slaughtered all livestock and frequently raped and killed, committing, as William Dalrymple points out, what would be considered war crimes today. At Istalif, a beautiful village that has had the good luck to be situated among wonderful mulberry trees, meadows and running brooks a short drive or, back then, ride, north of Kabul, and the bad luck to have been on the path of invading forces for about 3,000 years, "no males above 14 years were spared … Some of the men wanted to wreak their revenge on the women … the scene of plunder was dreadful … My eyes were shocked at the sight of a poor woman lying dead and a little infant of three or four months by her side, both its little thighs pierced and mangled by a musket ball," wrote young cavalry officer Neville Chamberlain.

The British, who had entered Afghanistan in the first place in part to further trade relations west of the East India Company's extant territory, destroyed the main Kabul bazaar, a stunning Mughal-era monument and the biggest commercial centre for a thousand or so miles around. Their point made, they then withdrew. On his last day in what would become Afghanistan, once borders were eventually drawn up, Chamberlain was cantering down the Khyber Pass with a fellow officer, John Nicholson, when they came across corpses. Among them was that of Nicholson's brother, naked and hacked to pieces, his genitals severed and stuffed in his mouth.

This is the third volume of Dalrymple's history of the British in south Asia in the early and mid-19th century. The first two – The Last Mughal and White Mughals – were deserved successes. This third shows all the elements we have come to expect from Dalrymple: the clear, fluid prose, the ability to give complex historical events shape, story and meaning, the use of new local sources to allow the voices of the people, in this case, of Kabul, Kohistan, the Khyber and elsewhere to be heard alongside the much-better documented accounts of the invaders, the deep knowledge and affection for the magnificently rich culture of the Mughals and their various copiers and a lack of patience with tiresome orientalist visions of the "proud Pashtun" or "noble Afghan". This is clear-eyed, non-judgmental, sober history, beautifully told.

Of Dalrymple's recent works, Return of a King is perhaps the most directly relevant to the present day. The author spent time in both Afghanistan and Pakistan during its research and elaborates on the obvious parallels with the current situation, as western forces prepare their own ignominious exit from Afghanistan. From the vain, ill-informed, militaristic civilian commander-in-chief surrounded by hawkish advisers impatient of the warnings given by better-informed men on the ground to the catalogue of errors – military, political, logistic – made once the invasion is under way, these are striking.

Any reader who has even the slightest awareness of what has been happening in the country since 2001 cannot avoid them.

Dalrymple is wrong to suggest the 2001 campaign in Afghanistan was as unnecessary as the 1839 invasion had been. Someone needed to dismantle the al-Qaida training camps in the country and the Taliban certainly weren't going to do so. Iraq was the war of choice that needed not to have been fought. But the incompetence, parsimony, waste and human cost of the following decade of international efforts in Afghanistan is certainly on a par with the previous episode. Through showing us the pity of one conflict, Dalrymple reveals that of the other. It remains to be seen if the legacy of this most recent intervention is as destructive.