They called it the "Death Star" because according to one source who worked inside it, "you could just reach out with a finger and eliminate" somebody. On the walls were banks of television screens, known by the special forces boys as "Kill TV", where footage from image-intensifier cameras of the enemy being blown up by air strikes, or being gunned down by undercover hit teams was shown.

This place was "the Machine", a state-of-the-art military command centre hidden away in an airbase in Balad, a desolate stretch of land north of Baghdad. It was created by Major General Stanley McChrystal, the chief of US Special Forces, the most secretive force in the American military. Here, in the permanently darkened communications cockpit, dozens of US and British (SAS) personnel would gather around as nightly raids took place against al-Qa'ida and their insurgent allies.

Sometimes McChrystal would lead the raids himself, his squad of elite undercover combat troops, known as Delta Force, being told at the last minute that the commander was coming along for the ride. No one was quite sure what the Pentagon policy was on two star generals going on such dangerous missions, but then very few people in the US Department of Defence, and even fewer outside it in Washington, were even aware of these shadowy operations going on in Iraq.

This was the secret and violent world which shaped Stanley McChrystal, who on Wednesday was sacked from his job as commander of Nato forces in Afghanistan. The irony, as his colleagues were pointing out yesterday, was that his spectacular downfall was not due to some illicit military action, but because of a magazine article his aides had arranged in order to publicise his most recent high-profile public career.

Nevertheless, the seeds of what was to bring him down may have been planted at his time running "black ops" [operations], the head of a close- knit team answerable to very few, where decisions were made about life and death on a daily basis. The autonomy was not just military. McChrystal and his men would go into the badlands – at that time most of Iraq – to make deals with local tribal leaders, pay out money, organise allies and informants. There was no question of practical civilian oversight as no diplomat, American or British, would venture into these areas.

Thus McChrystal and the group around him, many of whom would follow him to Kabul, would have little to do with US or British civilian leaders. Their mistrust of what one of them described to Rolling Stone magazine as, "the wimps in the White House" was almost inevitable because of the shadowy nature of their work. When they did meet the civilians, the men did not have much to say to them, because so much of what they knew was classified, and thus could not be imparted.

One American officer recalled for example how much McChrystal disliked entering the Green Zone, the heavily fortified conurbation in Baghdad where Western administrative officials were based. "Stan always looked uncomfortable, he hated all the red tape. I guess, if anything, he was happier talking to the Iraqis than to most of our own people from State [the State department]; he wasn't a networker." The General's preference for cultivating local leaders rather than Western officials continued in Afghanistan, where the President, Hamid Karzai, and others would speak of their regret at his departure.

But friends of McChrystal's say his time in Iraq should be put in context. It was the most ferocious period in the conflict following the US-led invasion, with Sunni and Shia militias killing each other and Iraqi and foreign troops. Armed criminal gangs were on the rampage, kidnapping and extorting money from an unprotected population.

McChrystal had been told, goes the lore: "The gates of hell had been opened and you have got to help to shut them". The US forces, stuck behind their heavily guarded bases, only able to move around in heavy armoured convoys, were not the answer: the war would have to be taken to the insurgents in the streets and fields.

Over cups of coffee at the main US base, Camp Victory, McChrystal described to fellow officers, like the British Lieutenant Colonel Richard Williams, then leading an SAS unit in Baghdad, his plans to carry out relentless rounds of night raids, killing or capturing insurgents, especially their commanders, and break the cycle of the militant groups "organically" reproducing themselves.

There were many figures among the coalition forces who questioned the approach. One senior British officer dismissed the notion that such "industrial counter-insurgency" could work. But the targeted attacks began, and along with prisoners came intelligence vital in the programme of "decapitation" against al-Qa'ida.

General McChrystal was in his element, eating just one meal a day, sleeping no more than four hours a night, constantly demanding more information on the militant networks. Among the few "artefacts" in his spartan accommodation was a prosthetic limb, belonging to a Sunni sheikh his men had gone to hunt but failed to find. The false arm had been abandoned during the man's hurried getaway.

However, questions were being asked about how information was being obtained. There was an unofficial inquiry into the treatment of detainees at Balad. McChrystal was absolved because he was not there when the alleged abuses had taken place.

But then came information from a captured suspect which vindicated the commander's approach in the eyes of the US military. Abu Musab al-Zarqawi, the leader of al-Qa'ida in Iraq, had been blamed for hundreds of bombings. It is highly unlikely that all of them were his group's handiwork, but the publicity helped create a myth of a master terrorist and Washington was demanding he must be stopped.

Intelligence agents working with McChrystal spent weeks winning the confidence of a suspect captured in the "Sunni Triangle" south of Baghdad. They eventually got a location where Zarqawi was staying, enabling the Americans to carry out an attack mortally wounding the al-Qa'ida leader. The body was brought to Balad. As it was being taken off the flight, McChrystal appeared, to stand and look at his enemy "as if trying some kind of silent communion", according to one of the crew members.

There were other successful operations, with the British SAS taking part in some of them, like the freeing of the missionary Norman Kember and Shia sheikhs supplying arms to be used against UK forces in Basra. Like their American boss, the SAS reported the bare minimum back to London.

In Afghanistan, McChrystal initiated policies which may have been an attempt to curb the lethal violence of his past. He brought in the doctrine of "courageous restraint" to minimise civilian casualties. He ordered air strikes, which had killed hundreds, to be significantly reduced in scale.

But he demanded control of the special forces operations, which had been run by a separate command under his predecessor, General David McKiernan. Iraq-style night raids dramatically increased, causing outcry from human rights groups, which complained that innocent civilians were often being killed by masked assailants.