Soon after dawn on May 11 1857, 150 years ago this week, the Mughal Emperor Bahadur Shah Zafar was saying his morning prayers in his oratory overlooking the river Jumna when he saw a cloud of dust rising on the far side of the river. Minutes later, he was able to see its cause: 300 East India Company cavalrymen charging wildly towards his palace.

The troops had ridden overnight from Meerut, where they had turned their guns on their British officers, and had come to Delhi to ask the emperor to give his blessing to their mutiny. As a letter sent out by the rebels' leaders subsequently put it: "The English are people who overthrow all religions ... As the English are the common enemy of both [Hindus and Muslims, we] should unite in their slaughter ... By this alone will the lives and faiths of both be saved."

The sepoys entered Delhi, massacred every Christian man, woman and child they could find and declared the 82-year-old emperor to be their leader. Before long the insurgency had snowballed into the largest and bloodiest anticolonial revolt against any European empire in the 19th century. Of the 139,000 sepoys of the Bengal army, all but 7,796 turned against the British. In many places the sepoys were supported by a widespread civilian rebellion.

There is much about British imperial adventures in the east at this time, and the massive insurgency it provoked, which is uneasily familiar to us today. The British had been trading in India since the early 17th century. But the commercial relationship changed towards the end of the 18th, as a new group of conservatives came to power in London, determined to make Britain the sole global power. Lord Wellesley, the brother of the Duke of Wellington and governor general in India from 1798 to 1805, called his new approach the Forward Policy. But it was in effect a project for a new British century. Wellesley made it clear he would not tolerate any European rivals, especially the French, and planned to remove any hostile Muslim regimes that might presume to resist the west's growing might.

The Forward Policy soon developed an evangelical flavour. The new conservatives wished to impose not only British laws but also western values on India. The country would be not only ruled but redeemed. Local laws which offended Christian sensibilities were abrogated - the burning of widows, for instance, was banned. One of the East India Company directors, Charles Grant, spoke for many when he wrote of how he believed providence had brought the British to India for a higher purpose: "Is it not necessary to conclude that our Asiatic territories were given to us, not merely that we draw a profit from them, but that we might diffuse among their inhabitants, long sunk in darkness, the light of Truth?"

The British progressed from removing threatening Muslim rulers to annexing even the most pliant Islamic states. In February 1856 they marched into Avadh, also known by the British as Oudh. To support the annexation, a "dodgy dossier" was produced before parliament, so full of distortions and exaggerations that one British official who had been involved in the operation described the parliamentary blue book (or paper) on Oudh as "a fiction of official penmanship, [an] Oriental romance" that was refuted "by one simple and obstinate fact", that the conquered people of Avadh clearly "preferred the slandered regime" of the Nawab "to the grasping but rose-coloured government of the company".

The reaction to this came with the great mutiny, or as it is called in India, the first war of independence. Though it reflected many deeply held political and economic grievances, particularly the feeling that the heathen foreigners were interfering with a part of the world to which they were alien, the uprising was consistently articulated as a defensive action against the inroads missionaries and their ideas were making in India, combined with a generalised fight for freedom from western occupation.

Although the great majority of the sepoys were Hindus, there are many echoes of the Islamic insurgencies the US fights today in Iraq and Afghanistan. In Delhi a flag of jihad was raised in the principal mosque, and many of the resistance fighters described themselves as mujahideen or jihadis. There was even a regiment of "suicide ghazis" who vowed to fight until they met death.

Events reached a climax on September 14 1857, when British forces attacked the besieged city. They proceeded to massacre not only the rebel sepoys and jihadis, but also the ordinary citizens of the Mughal capital. In one neighbourhood alone, Kucha Chelan, 1,400 unarmed citizens were cut down. Delhi, a sophisticated city of half a million souls, was left an empty ruin.

The emperor was put on trial and charged, quite inaccurately, with being behind a Muslim conspiracy to subvert the empire stretching from Mecca and Iran to Delhi's Red Fort. Contrary to evidence that the uprising broke out first among the overwhelmingly Hindu sepoys, the prosecutor argued that "to Musalman intrigues and Mahommedan conspiracy we may mainly attribute the dreadful calamities of 1857". Like some of the ideas propelling recent adventures in the east, this was a ridiculous and bigoted oversimplification of a more complex reality. For, as today, western politicians found it easier to blame "Muslim fanaticism" for the bloodshed they had unleashed than to examine the effects of their own foreign policies. Western politicians were apt to cast their opponents in the role of "incarnate fiends", conflating armed resistance to invasion and occupation with "pure evil".

Yet the lessons of 1857 are very clear. No one likes people of a different faith conquering them, or force-feeding them improving ideas at the point of a bayonet. The British in 1857 discovered what the US and Israel are learning now, that nothing so easily radicalises a people against them, or so undermines the moderate aspect of Islam, as aggressive western intrusion in the east. The histories of Islamic fundamentalism and western imperialism have, after all, long been closely and dangerously intertwined. In a curious but very concrete way, the fundamentalists of all three Abrahamic faiths have always needed each other to reinforce each other's prejudices and hatreds. The venom of one provides the lifeblood of the others.

· William Dalrymple's The Last Mughal: The Fall of a Dynasty, Delhi 1857, has just been published in paperback by Bloomsbury

williamdalrymple.com