In the early summer of 1980, the Texas Congressman Charlie Wilson walked off the floor of the House of Representatives into the Speaker's Lobby. A Teletype at one end spewed out stories from AP, UPI and Reuters. Wilson was a news junkie, and he reached down and began reading a story datelined from Kabul.

The article described hundreds of thousands of refugees fleeing Afghanistan as Soviet helicopter gunships levelled villages, slaughtered livestock, and killed anyone who harboured guerrillas resisting the occupation. What caught Wilson's attention, however, was the reporter's conclusion that the Afghan warriors were refusing to quit. The article described how they were murdering Russians in the dead of night with knives and pistols, hitting them over the head with shovels and stones. Against all odds, there was a growing rebellion underway against the Red Army.

It would have been a sobering insight for the Communist rulers if they could have followed what happened in the few minutes after Wilson finished reading the Associated Press dispatch. The mysterious force in the US government that was destined to hound the Red Army with a seemingly limitless flood of ever more lethal weapons was about to be activated.

Download the new Independent Premium app Sharing the full story, not just the headlines

No one, however, was paying attention, not even in the American government, when Wilson picked up a phone and called the Appropriations Committee staffer who dealt with "black appropriations", the CIA funds.

The congressman knew enough about the eccentric workings of the subcommittee to know when a member can act alone to fund a programme. "How much are we giving the Afghans?" he asked.

"Five million," said the staffer.

There was a moment's silence. "Double it," said the Texan.

So far as anyone can tell, no congressman prior to Charlie Wilson had ever moved unsolicited to increase a CIA budget. From the beginning of the Cold War, Congress had granted that exclusive right to the president. But as dramatic as the doubling might sound, it had no visible impact on the war. It wasn't reported or debated, and it never even registered on the KGB's radar screen in Russia. At best, all it did was provide the mujahedin with a few thousand more Enfield rifles and perhaps some machine guns, so that they could go out and die for their faith in greater numbers.

Wilson's intervention had not cost the congressman much more than a telephone call to a key staffer and a few additional minutes when the subcommittee met to appropriate the nation's secret intelligence budget. It was an impulsive action, a personal gesture to bolster a painfully inadequate US programme.

Wilson so easily crossed the line into this covert arena that no one stopped to question his right to be there or worry about the precedent he might be setting. It would be another two years before he would return to put this precedent to the test. But this is where he first demonstrated that there could be another power centre in the American government, one that could act in a way that was totally unpredictable to drive a US covert policy.

Years later, as he tried to explain how the CIA ended up with a billion dollars a year to kill Russian soldiers in Afghanistan, Wilson's CIA co-conspirator Gust Avrakotos (played in the new film by Philip Seymour Hoffman) would offer a curious explanation. "It began with a Texas woman, one of Wilson's contributors. She's the one who got him interested." Joanne Harring (played by Julia Roberts) was a glamorous and exotic figure from the oil-rich world of Texas in the 1970s and 1980s. At the time nobody imagined that, in addition to her role as a social lioness and hostess to the powerful, she was simultaneously responsible for setting in motion a process that would profoundly impact the outcome of the Afghan war. When almost everyone had written off the Afghans as a lost cause, she saw potential for greatness in the most unlikely characters. In the pivotal first years of the jihad, she became both matchmaker and muse to Pakistan's Muslim fundamentalist military dictator, Zia ul-Haq, as well as to Charlie Wilson.

Most of the women Wilson was seeing in those days and there were many were half Herring's age. But Joanne Herring was a woman of extraordinary resources who knew how to mesmerise a man on many levels not the least of which was her ability to sweep this congressman from the Bible Belt into her dazzling world of black-tie dinners, movie stars, countesses, Saudi princes and big-time Republican oil magnates. Invariably, when reporters wrote features about Joanne Herring, they invoked Scarlett O'Hara.

As the romance bloomed, Herring found herself reborn as a ferocious champion of Zia and the Afghans, and she became convinced that Wilson was the one who could save the day. Six months earlier, she had been at home when a message from Afghanistan came in "via the underground". It was from her friend Charles Fawcett, a note scribbled with crayons on the back of a child's notebook:

"Come immediately. Bring film equipment. The world doesn't know what's going on here."

It was hard for the congressman not to be impressed as he listened to Joanne describe how she had left immediately for Islamabad and then crossed into the war zone with Fawcett. "All this had to be very secretive," she whispered conspiratorially.

"They dressed me like a man. I had a bodyguard who was seven feet tall with a handlebar mustache and an Enfield rifle." At one point, Joanne told Wilson, this giant moved her about in a barrel to hide her. "It was so cold that all the men gave me their blankets. But it was like sleeping under a dead hippo. I was so cold, it was horrible, but it was the most exciting thing in my life."

As she told this story to Wilson, she played on themes she knew would move his Texan spirit. She described how these primitive tribesmen would bow to Mecca in prayer five times a day. She emphasised how few weapons they had and described how the Afghans treated their guns like library books as soon as one warrior crossed the border, he would turn in his gun, handing it over to another man going off to face death. "It was so humbling," she went on. "Nothing ever affected me like seeing those 20,000 men raising their guns and shouting to fight to the last drop of their blood."

By 1982, Peshawar, the capital of Pakistan's historic North-West Frontier, had also become the not-very-secret centre of the Afghan resistance. It was only 30 miles from the Afghan border and minutes from the sprawling refugee camps. It was also home to the leaders of the seven mujahedin military parties that the US Central Intelligence Agency and Pakistan's Inter-Services Intelligence Directorate (ISI) had created to organise the war effort. But no one offered to take Wilson to visit these secret warriors. His schedule on his first trip to Peshawar called for the traditional tour of the UN-supported refugee camps, a scene that appalled everyone who came to Peshawar: millions of proud Afghans living in mud huts without running water or the ability to feed themselves. In the month of his visit 20,000 more had poured in young boys and girls dressed in bright tribal clothing; the women with their faces covered. They came from the mountains and valleys of a country where their ancestors had lived for centuries, a legendary warrior nation not easy to intimidate and uproot.

All brought horror stories of what had caused them to flee their country. In particular they talked of helicopter gunships that hovered over their villages, hounding them even as they fled. It began to dawn on Wilson that there were only Afghans in this part of Pakistan and that he was witnessing an entire nation in flight from the Communists. This spectacle of mass suffering roused him, but he had been to refugee camps before and for him there was something almost impersonal about such a mass of humanity. What did catch his attention that day was the absence of men no teenagers, not even 40- or 50-year-olds. He was told they were all fighting in the jihad.

It was at his next stop, the Red Cross hospital on the edge of Peshawar, that he lost his heart to the Afghans. Scores of young men were laid out on hospital beds. The doctors sat with Wilson at the bed of a young boy and explained that his hand had been blown off by a Russian butterfly mine designed to look like a toy. This threw Wilson into a rage. A young Afghan who had stepped on a land mine explained he was proud of his sacrifice. "He told me his only regret was that he couldn't have his feet grown back so he could go kill Russians."

Wilson moved from bed to bed, undone by the carnage but increasingly aware of why most of them were there. He spoke to a wounded commander as the effects of an anaesthetic started to kick in. The man was waving his hand in a circle, describing the horror of the Russian gunship that had put him there. Not one of them complained about their lost limbs. But every one of them described their fury at the Russian gunships. And to a man, they asked for only one thing a weapon to bring down this tool of Satan. Wilson wanted desperately to give something to these warriors and, before leaving, he donated a pint of his blood.

His next stop was a meeting with a council of Afghan elders, hundreds of whom were waiting for him in a huge colourful tent, decorated with cotton fabrics that looked like floating oriental rugs. As he walked in, Wilson was dazed by the sight of long white beards and turbans, and the men's fierce, unblinking eyes. The Pakistanis had told them that the congressman had come as a friend offering assistance, and as he entered they shouted, "Allahu Akbar" God is Great.

To Wilson it was like a scene from the Old Testament. When the elders invited the Texan to speak, he delivered what he thought would be just the right message. "I told them that they were the most courageous people in the world and I said, 'We're going to help you. None of your families will suffer from lack of shelter and food.' I pledged that their soldiers would not be left to die in agony and that we would give them millions in humanitarian assistance."

An old man rose to respond. He told Wilson he could keep his bandages and rice. What they needed was a weapon to destroy the gunships. It was at this moment that Wilson realised he was in the presence of a people who didn't care about sympathy. They didn't want medicine or charity. They wanted revenge.

Charlie Wilson delivered it. Throughout the 1980s the Afghan mujahedin were America's surrogate soldiers in the brutal guerrilla war that became the Soviet Union's Vietnam, a defeat that helped trigger the subsequent collapse of the Communist empire. Afghanistan was a secret war that the CIA fought and won without debates in Congress or protests in the street. It was not just the CIA's biggest operation, it was the biggest secret war in history. In the course of a decade, billions of rounds of ammunition and hundreds of thousands of weapons were smuggled across the border on the backs of camels, mules, and donkeys. At one point more than 300,000 fundamentalist Afghan warriors carried weapons provided by the CIA; thousands were trained in the art of urban terror. Before it was over, 28,000 Soviet soldiers had been killed.

It was January 1989, just as the Red Army was preparing to withdraw its last soldiers from Afghanistan, when Wilson called to invite me to join him on a fact-finding tour of the Middle East. I had produced a CBS 60 Minutes profile of him several months earlier and had no intention of digging further into his role in the Afghan war.

There were two surprises on that trip, revelations that opened my eyes to a bigger story: the first was the princely reception given to Wilson wherever he went in the Arab world. The second was my introduction to Avrakotos, who had recently retired from the Agency and was reunited with Wilson for the first time in several years. As we moved from Kuwait down to Basra, where hundreds of thousands had died in the closing battles of the Iran-Iraq War, I began talking to Avrakotos, and in short order I realised that the Afghan campaign had been anything but a typical CIA programme.

When our commercial flight back to Baghdad was cancelled, Avrakotos managed to get us on to a lavish Boeing 707 owned by a Saudi religious leader by telling him about Wilson's role in the Afghan war. In Riyadh, a royal receiving party met us at the airport. A caravan of brand-new white Mercedes, complete with police escort, swept us off to the palace for a meeting with the king's brother, the Saudi defence minister Prince Sultan. After tea, Wilson delivered his message: he had come to thank the Saudi royal family for its extraordinary generosity in matching the Americans dollar for dollar in Afghanistan. It became clear that the gratitude went both ways when Wilson was shown to his quarters several hours later a preposterously luxurious suite with a living room that seemed to be the size of a football field.

"We want you to know, Mr Congressman," the prince's aide said, "that these are larger quarters than we provided for George Bush. Mr Bush is only the vice president. You won the Afghan war."

Throughout the Muslim world, the victory of the Afghans over the army of a modern superpower was seen as a transformational event. But back home, no one seemed to be aware that something important had taken place and that the United States had been the moving force behind it. Any chance of an American appreciation for the Afghan miracle was fast disappearing, as one incredible event after another began to unravel the Soviet bloc. That August, Lech Walesa and his movement pushed aside the Communists and took power in Poland. Then in November, the ultimate symbol of Communist oppression, the Berlin Wall, came down. It was just nine months after the Red Army's humiliating retreat from Afghanistan, and the dominoes were now falling in central and Eastern Europe. As Charlie Wilson saw it, his Afghans had played a decisive role in helping to trigger and hasten the collapse of the Communist eastern bloc. More than a million Afghans had died, and no one had ever thanked them for their sacrifice.

Throughout the war, Wilson had always told his colleagues that Afghanistan was the one morally unambiguous cause that the United States had supported since the Second World War and never once had any member of Congress stood up to protest or question the vast expenditures. But with the departure of the Soviets, the war was anything but morally unambiguous. By 1990 the Afghan freedom fighters had suddenly and frighteningly gone back to form, re-emerging as nothing more than feuding warlords obsessed with settling generations-old scores. The difference was that they were now armed with hundreds of millions of dollars' worth of weapons and explosives of every conceivable type. The justification for the huge CIA operation had been to halt Soviet aggression, not to take sides in a tribal war certainly not to transform the killing capacity of these warriors.

What now seems clear is that, under the umbrella of the CIA's programme, Afghanistan had become a gathering place for militant Muslims from around the world. As early as the Gulf War, Gulbuddin Hekmatyar, long the main recipient of CIA weaponry, had articulated his belief that the US wanted world domination and control of Muslim oil. A man Charlie once described as "goodness personified", Jalaluddin Haqqani, had long been a gateway for Saudi volunteers, and for years the CIA had no problem with such associations. Osama bin Laden was one of those volunteers who could frequently be found in the same area where Charlie had been Haqqani's honoured guest.

The presumption at Langley had been that when the United States packed its bags and cut off the Afghans, the jihad would simply burn itself out. If the Afghans insisted on killing one another, it would be a shame but not America's problem. Perhaps that policy would have worked out had it been only weapons that we left behind. But the more dangerous legacy of the Afghan war is found in the minds and convictions of Muslims around the world. To them the miracle victory over the Soviets was all the work of Allah not the billions of dollars that America and Saudi Arabia poured into the battle, not the 10-year commitment of the CIA that turned an army of primitive tribesmen into technoholy warriors. The consequence for America of having waged a secret war and never acknowledging or advertising its role was that it set in motion the spirit of jihad and the belief in surrogate soldiers that, having brought down one superpower, they could just as easily take on another.

The morning of 11 September 2001 broke bright and shining in the capital of the United States. As was his custom before leaving for work, Charlie Wilson walked out on to his terrace to take in the spectacular view. Never in history had a nation accumulated such dominance over the rest of the world as the US had in the decade following the Soviet collapse. Wilson's name was all but unknown to most Americans, but as he looked out over the monuments and the historic houses of government, he had every reason to believe that he had played a part in the startling disappearance of America's greatest enemy.

A call from a friend interrupted his morning ritual: "Do you have your television on?" The sight of the World Trade Center in flames stunned him, but like most Americans, he assumed it had to have been a horrendous accident. Some 10 minutes later he was watching when the second plane appeared on screen and flew straight into the second tower. A sickening realisation gripped him: it had to be the work of terrorists, and, if so, he had little doubt that the killers were Muslims.

"I didn't know what to think, but figured if I got downtown I could learn more." By then Wilson had retired from Congress and was working as a lobbyist, with Pakistan as one of his main accounts. At 9.43am, half an hour after the first attack, he was driving across the Fourteenth Street Bridge with the windows up and news radio blasting so loud that he didn't hear the explosion that rocked the Pentagon less than a mile away.

He didn't know what to make of it all at first. When the photographs of the 19 hijackers appeared in newspapers across the country, he took some comfort in pointing out that they were all Arabs, not Afghans. "It didn't register with me for a week or two that this thing was all based in my mountains."

For most Americans, the events of September 11 were quickly tied to Afghanistan when it was learned that the hijackers had all spent time there. Much was made of this by the Bush administration, which assailed the Taliban for harbouring Osama bin Laden and for allowing Afghanistan to become a breeding ground for international terrorists. The American public rallied behind the president when he launched his "war on terror". But almost everyone seemed confused about who the terrorists were, and all but clueless to explain why they hated the United States so much.

The question is not so difficult to understand if you put yourself in the shoes of the Afghan veterans in the aftermath of the Soviet departure. Within months, the US government "discovered" what it had known for the past eight years that Pakistan was hard at work on the Islamic bomb. (The dirty little secret of the Afghan war was that Zia had extracted a concession early on from President Reagan: Pakistan would work with the CIA against the Soviets in Afghanistan, and in return the United States would provide massive aid but would agree to look the other way on the question of the bomb.) But with the Russians gone, sanctions were imposed and all military and economic assistance was cut off. Within a year, the Clinton administration would move to place Pakistan on the list of state sponsors of terrorism. The Pakistan military had long been the surrogates for the CIA, and every Afghan and Arab mujahedin fighter came to believe that America had betrayed the Pakistanis. And when the United States kept its troops (including large numbers of women) in Saudi Arabia, not just bin Laden but most Islamists believed that America wanted to seize the Islamic oil fields and was seeking world domination.

By the end of 1993, in Afghanistan itself there were no roads, no schools, just a destroyed country and the United States was washing its hands of any responsibility. It was in this vacuum that the Taliban and Osama bin Laden would emerge as the dominant players. It is ironic that bin Laden, a man who had had almost nothing to do with the victory over the Red Army, would come to personify the power of the jihad.

It's not what Wilson had in mind when he took up the cause of the Afghans. Nevertheless, in spite of September 11 and the horrors that have flowed from it, he steadfastly maintains that it was all worth it and that nothing can diminish what the Afghans accomplished for America and the world with their defeat of the Red Army: "I truly believe that this caused the Berlin Wall to come down a good five, maybe 10 years before it would have otherwise. At least 100 million Eastern Europeans are breathing free today, to say nothing of the Russian people. It's the truth, and all those people who are enjoying those freedoms have no idea of the part played by a million Afghan ghosts. To this day no one has ever thanked them.

"They removed the threat we all went to sleep with every night, of World War III breaking out. The countries that used to be in the Warsaw Pact are now in Nato. These were truly changes of biblical proportions, and the effect the jihad had in accelerating these events is nothing short of miraculous.

"These things happened. They were glorious and they changed the world. And the people who deserved the credit are the ones who made the sacrifice. And then we fucked up the endgame."

The story of Charlie Wilson and the CIA's secret war in Afghanistan is an important, missing chapter of our recent past. Ironically, neither the United States government nor the forces of Islam will want this history to be known. But the full story of America's central role in the Afghan jihad needs to be told and understood for any number of reasons. Clearly it's not helpful for the world of militant Islam to believe that its power is so great that nothing can stop it. But the danger exists for us as well. It may not be welcomed by a government that prefers to see the rising tide of Islamic militancy as having no connection to our policies or our actions. But the terrible truth is that the group of sleeping lions that the United States roused may well have inspired an entire generation of militant young Muslims to believe that the moment is theirs.

This article is an edited extract from 'Charlie Wilson's War' by George Crile, published by Atlantic Books, 8.99. The film is released on 11 January 2008