The Return of a King: The Battle for Afghanistan, 1839-42. By William Dalrymple. Bloomsbury; 608 pages; £24.99. To be published in America in April by Knopf; $30. Buy from Amazon.com, Amazon.co.uk

IN 1842 some 700 European soldiers, 3,800 Indian sepoys and 14,000 civilian staff fled Kabul in the deep chill of winter. The British occupation of Afghanistan, in place since 1839, was no longer tenable. A week later a single survivor from this fleet staggered into view at the British-held fort at Jalalabad in eastern Afghanistan. This lone soldier on a collapsing pony, as depicted in Elizabeth Butler’s 1879 painting “Remnants of an Army” (pictured), has become a lasting image of the first Anglo-Afghan war. It is an appropriately bleak one.

It was “a war begun for no wise purpose”—and one that need never have taken place. It would stand as the worst British military disaster until the fall of Singapore exactly a century later. William Dalrymple, a British historian, recounts Britain’s early misadventures in Afghanistan in “Return of a King”, a masterful history. This is a story that hangs heavy with imperial overconfidence, political incompetence and wilful bureaucratic misjudgment. And as the latest occupying force in Afghanistan negotiates its exit, this chronicle seems all too relevant now.

Afghanistan in the early 19th century was insignificant. Though home to the largest market in Central Asia, it was wretchedly poor, and its rulers barely clung to power. But the country was also a vital gateway for invasions into India. Fearing a Russian attack on its most treasured possession, Britain looked to secure Kabul.

It would have been easy simply to make a pact with Afghanistan’s ruler, Dost Mohammad. Instead Britain backed Shah Shuja, the deposed king, who had been living in exile in India for three decades. Thus began the “Great Game”, an entirely unnecessary competition for Afghanistan between Russia and Britain, conjured up by armchair polemicists in London.

The war began promisingly. The grandly named Army of the Indus lumbered off to Afghanistan with 58,000 people, 30,000 camels (300 for the wine alone) and a pack of foxhounds for hunting. The force took Kabul with relative ease and restored Shuja to the throne; he was accepted with little turmoil. But the infidel occupation soon proved unpopular. Shuja was swiftly seen as a puppet of the invading army. A local chieftain inquired of the British, “You have brought an army into the country. But how do you propose to take it out again?” It remains a tough question to answer.

The occupation grew entrenched. Families joined officers and tried to make themselves comfortable. In one case this meant bringing a grand piano; in another a cat, a parakeet and five maidservants. And the soldiers did little to endear themselves, but rather meddled in religious affairs, created a thriving market for prostitutes and helped generate inflation. Afghans grew restless, and within a year rebellions started breaking out.

A series of tactical mistakes followed. Assuming the conquest was complete, Britain withdrew large portions of the army in preparation for the brewing opium war in China. Reports of growing discontent were disregarded, and no money went towards new defences in Kabul. The final straw came when the British cut payments to the tribes who guarded vital supply routes, guaranteeing widespread revolts. Much of the carnage that followed was conducted in the religious name of jihad—a “relative innovation” in Afghanistan as previous wars had been largely between Muslims. Ultimately the struggle ended as it began, with Dost Mohammad in power at Kabul.

The war cost £15m—about £50 billion ($80 billion) in today’s money—and the lives of 40,000 people, 50,000 camels and at least one cat. That is still a bargain compared with the current conflict, which costs America more than $100 billion every year.

“Return of a King” confirms Mr Dalrymple’s reputation as a gifted historian and an engaging writer. But the signal achievement of this work is that it makes a nearly two-century-old war seem disturbingly fresh. It makes for grim reading. Like the current adventure in Afghanistan, this first one was undone by the unsustainable cost of occupation, waning political and public interest, and the need to divert resources. In the race to secure the country, the British ambassador in Tehran believed “that he who is not with us is against us”. “Operation Infinite Justice”, the abandoned name for the 21st-century war, carries echoes of the “Army of Retribution” sent in after the disastrous British retreat.

Yet there is one parallel that Mr Dalrymple leaves his readers to draw, which is the futility of treating a diffuse enemy like a traditional state. By using a wealth of unmined sources, including Afghan epic poetry, official court histories and Shuja’s own biography, he brings out the “deeply fractured” nature of the resistance, in which “different groups under different commanders…acted…with only the bare minimum of co-ordination”.

Today’s bloody conflict in Afghanistan is scarcely different, with a complex web of allegiances fiercely fighting the occupation. Few lessons have been learned from past mistakes. Mr Dalrymple’s book is a timely reminder of the way that wars can begin with promise but end in disgrace.