On a hot summer morning 18 months ago a team of four Americans – two FBI agents and two army officers – rolled into Ghazni, a dusty town 50 miles south of Kabul. They had come to interview two unusual prisoners: a woman in a burka and her 11-year-old son, arrested the day before.

Afghan police accused the mysterious pair of being suicide bombers. What interested the Americans, though, was what they were carrying: notes about a "mass casualty attack" in the US on targets including the Statue of Liberty and a collection of jars and bottles containing "chemical and gel substances".

At the town police station the Americans were directed into a room where, unknown to them, the woman was waiting behind a long yellow curtain. One soldier sat down, laying his M-4 rifle by his foot, next to the curtain. Moments later it twitched back.

The woman was standing there, pointing the officer's gun at his head. A translator lunged at her, but too late. She fired twice, shouting "Get the fuck out of here!" and "Allahu Akbar!" Nobody was hit. As the translator wrestled with the woman, the second soldier drew his pistol and fired, hitting her in the abdomen. She went down, still kicking and shouting that she wanted "to kill Americans". Then she passed out.

Whether this extraordinary scene is fiction or reality will soon be decided thousands of miles from Ghazni in a Manhattan courtroom. The woman is Dr Aafia Siddiqui, a Pakistani neuroscientist and mother of three. The description of the shooting, in July 2008, comes from the prosecution case, which Siddiqui disputes. What isn't in doubt is that there was an incident, and that she was shot, after which she was helicoptered to Bagram air field where medics cut her open from breastplate to bellybutton, searching for bullets. Medical records show she barely survived. Seventeen days later, still recovering, she was bundled on to an FBI jet and flown to New York where she now faces seven counts of assault and attempted murder. If convicted, the maximum sentence is life in prison.

The prosecution is but the latest twist in one of the most intriguing episodes of America's "war on terror". At its heart is the MIT-educated Siddiqui, once declared the world's most wanted woman. In 2003 she mysteriously vanished for five years, during which time she was variously dubbed the "Mata Hari of al-Qaida" or the "Grey Lady of Bagram", an iconic victim of American brutality.

Yet only the narrow circumstances of her capture – did she open fire on the US soldier? – are at issue in the New York court case. Fragile-looking, and often clad in a dark robe and white headscarf, Siddiqui initially pleaded not guilty, insisting she never touched the soldier's gun. Her lawyers say the prosecution's dramatic version of the shooting is untrue. Now, after months of pre-trial hearings, she appears bent on scuppering the entire process.

During a typically stormy hearing last Thursday, Siddiqui interrupted the judge, rebuked her own lawyers and made strident appeals to the packed courthouse. "I am boycotting this trial," she declared. "I am innocent of all the charges and I can prove it, but I will not do it in this court." Previously she had tried to fire her lawyers due to their Jewish background (she once wrote to the court that Jews are "cruel, ungrateful, back-stabbing" people) and demanded to speak with President Obama for the purpose of "making peace" with the Taliban. This time, though, she was ejected from the courtroom for obstruction. "Take me out. I'm not coming back," she said defiantly.

The trial, due to start in January, is just one piece of a much larger ­ puzzle. It is a tale of spies and militants, disappearance and deception, which has played out in the shadowlands of Pakistan and Afghanistan since 2001. In search of answers I criss-crossed Pakistan, tracking down Siddiqui's relatives, retired ministers, shadowy spy types and pamphleteers. The truth was maddeningly elusive. But it all started in Karachi, the sprawling port city on the Arabian Sea where Siddiqui was born 37 years ago.

Her parents were Pakistani strivers – middle-class folk with strong faith in Islam and education. Her father, Mohammad, was an English-trained doctor; her mother, Ismet, befriended the dictator General Zia ul-Haq. Aafia was a smart teenager, and in 1990 followed her older brother to the US. Impressive grades won her admission to the prestigious Massachusetts Institute of Technology and, later, Brandeis University, where she graduated in cognitive neuroscience. In 1995 she married a young Karachi doctor, Amjad Khan; a year later their first child, Ahmed, was born.

Siddiqui was also an impassioned Muslim activist. In Boston she campaigned for Afghanistan, Bosnia and Chechnya; she was particularly affected by graphic videos of pregnant Bosnian women being killed. She wrote emails, held fundraisers and made forceful speeches at her local mosque. But the charities she worked with had sharp edges. The Nairobi branch of one, Mercy International Relief Agency, was linked to the 1998 US embassy bombings in east Africa; three other charities were later banned in the US for their links to al-Qaida.

The September 11 2001 attacks marked a turning point in Siddiqui's life. In May 2002 the FBI questioned her and her husband about some unusual internet purchases they had made: about $10,000 worth of night-vision goggles, body armour and 45 military-style books including The Anarchist's Arsenal. (Khan said he bought the equipment for hunting and camping expeditions.) Their marriage started to crumble. A few months later the couple returned to Pakistan and divorced that August, two weeks before the birth of their third child, Suleman.

On Christmas Day 2002 Siddiqui left her three children with her mother in Pakistan and returned to the US, ostensibly to apply for academic jobs. During the 10-day trip, however, Siddiqui did something controversial: she opened a post box in the name of Majid Khan, an alleged al-Qaida operative accused of plotting to blow up petrol stations in the Baltimore area. The post box, prosecutors later said, was to facilitate his entry into the US.

Six months after her divorce, she married Ammar al-Baluchi, a nephew of the 9/11 mastermind, Khalid Sheikh Mohammed, at a small ceremony near Karachi. Siddiqui's family denies the wedding took place, but it has been confirmed by Pakistani and US intelligence, al-Baluchi's relatives and, according to FBI interview reports recently filed in court, Siddiqui herself. At any rate, it was a short-lived honeymoon.

Fowzia Siddiqui is the elder sister of Aafia Siddiqui. Photograph: Declan Walsh

In March 2003 the FBI issued a global alert for Siddiqui and her ex-husband, Amjad Khan. Then, a few weeks later, she vanished. According to her family, she climbed into a taxi with her three children – six-year-old Ahmed, four-year-old Mariam and six-month old Suleman – and headed for Karachi airport. They never made it. (Khan, on the other hand, was interviewed by the FBI in Pakistan, and subsequently released.)

Initially it was presumed that Siddiqui had been picked up by Pakistan's Inter-Service Intelligence (ISI) spy agency at the behest of the CIA. The theory seemed to be confirmed by American media reports that Siddiqui's name had been given up by Mohammed, the 9/11 instigator, who was captured three weeks earlier. (If so, Mohammed was probably speaking under duress – the CIA waterboarded him 183 times that month.)

There are several accounts of what happened next. According to the US government, Siddiqui was at large, plotting mayhem on behalf of Osama bin Laden. In May 2004 the US attorney general, John Ashcroft, listed her among the seven "most wanted" al-Qaida fugitives. "Armed and dangerous," he said, describing the Karachi woman as a terrorist "facilitator" who was willing to use her education against America. "Al-Qaida Mom" ran the headline in the New York Post.

But Siddiqui's family and supporters tell a different story. Instead of plotting attacks, they say, Siddiqui spent the missing five years at the dreaded Bagram detention centre, north of Kabul, where she suffered unspeakable horrors. Yvonne Ridley, the British journalist turned Muslim campaigner, insists she is the "Grey Lady of Bagram" – a ghostly female detainee who kept prisoners awake "with her haunting sobs and piercing screams". In 2005 male prisoners were so agitated by her plight, she says, that they went on hunger strike for six days.

For campaigners such as Ridley, Siddiqui has become emblematic of dark American practices such as abduction, rendition and torture. "Aafia has iconic status in the Muslim world. People are angry with American imperialism and domination," she told me.

But every major security agency of the US government – army, FBI, CIA – denies having held her. Last year the US ambassador to Islamabad, Anne Patterson, went even further. She stated that Siddiqui was not in US custody "at any time" prior to July 2008. Her language was unusually categoric.

To reconcile these accounts I flew to Siddiqui's hometown of Karachi. The family lives in a spacious house with bougainvillea-draped walls in Gulshan Iqbal, a smart middle-class neighbourhood. Inside I took breakfast with her sister, Fowzia, on a patio overlooking a toy-strewn garden.

As servants brought piles of paratha (fried bread), Fowzia produced photos of a smiling young woman whom she described as the victim of an international conspiracy. The US had been abusing her sister in Bagram, she said, then produced her for trial as part of a gruesome justice pageant. "As far as I'm concerned this trial [in New York] is just a great drama. They write the script as they go. I've stopped asking questions," she said resignedly.

But Fowzia, a Harvard-educated neurologist, was frustratingly short on hard information. She responded to questions about Aafia's whereabouts between 2003 and 2008 with cryptic cliches. "It's not that we don't know. It's that we don't want to know," she said. And she blamed reports of al-Qaida links on a malevolent American press. "Half of them work for the CIA," she said.

The odd thing, though, was that the person who might unlock the entire mystery was living in the same house. After being captured with his mother in Ghazni last year, 11-year-old Ahmed Siddiqui was flown back to Pakistan on orders from the Afghan president, Hamid Karzai. Since then he has been living with his aunt Fowzia. Yet she has forbidden him from speaking with the press – even with Yvonne Ridley – because, she told me, he was too traumatised.

"You tell him to do something but he just stands there, staring at the TV," she said, sighing heavily. But surely, I insisted, after 15 months at home the boy must have divulged some clue about the missing years?

Fowzia's tone hardened. "Ahmed's not allowed to speak to the press. That was part of the deal when they gave him to us," she said firmly.

"Who are they?" I asked.

She waved a finger in the air. "The network. Those who brought him here."

Moments later Fowzia excused herself. The interview was over. As she walked me to the gate, I was struck by another omission: Fowzia had barely mentioned Ahmed's 11-year-old sister, Mariam, or his seven-year-old brother, Suleman, who are still missing. Amid the hullabaloo about their imprisoned mother, Aafia's children seemed to be strangely forgotten.

That night I went to see Siddiqui's ex-husband, Amjad Khan. He ushered me through a deathly quiet house into an upstairs room where we sat cross-legged on the floor. He had a soft face under the curly beard that is worn by devout Muslims. I recounted what Fowzia told me. He sighed and shook his head. "It's all a smokescreen," he said. "She's trying to divert your attention."

The truth of the matter, he said, was that Siddiqui had never been sent to Bagram. Instead she spent the five years on the run, living clandestinely with her three children, under the watchful eye of Pakistani intelligence. He told me they shifted between Quetta in Baluchistan province, Iran and the Karachi house I had visited earlier that day. It was a striking explanation. When I asked for proof, he started at the beginning.

Their parents, who arranged the marriage, thought them a perfect match. The couple had a lot in common – education, wealth and a love for conservative Islam. They were married over the phone; soon after Khan moved to America. But his new wife was a more fiery character than he wished. "She was so pumped up about jihad," he said.

Six months into the marriage, Siddiqui demanded the newlyweds move to Bosnia. Khan refused, and grew annoyed at her devotion to activist causes. During a furious argument one night, he told me, he flung a milk bottle at his wife that split her lip.

After 9/11 Aafia insisted on returning to Pakistan, telling her husband that the US government was forcibly converting Muslim children to Christianity. Later that winter she pressed him to go on "jihad" to Afghanistan, where she had arranged for them to work in a hospital in Zabul province. Khan refused, sparking a vicious row. "She went hysterical, beating her hands on my chest, asking for divorce," he recalled.

After Siddiqui disappeared in March 2003, Khan started to worry for his children – he had never seen his youngest son, Suleman. But he was reassured that they were still in Pakistan through three sources. He hired people to watch her house and they reported her comings and goings. His family was also briefed by ISI officials who said they were following her movements, he said. (Khan named an ISI brigadier whom I later contacted; he declined to speak).

Most strikingly, Khan claimed to have seen his ex-wife with his own eyes. In April 2003, he said, the ISI asked him to identify his ex-wife as she got off a flight from Islamabad, accompanied by her son. Two years later he spotted her again in a Karachi traffic jam. But he never went public with the information. "I wanted to protect her, for the sake of my children," he said.

Shams ul-Hassan Faruqi, a geologist and uncle of Dr Aafia Siddiqui, at his home in Islamabad, Pakistan Photograph: Declan Walsh

Khan's version of events has enraged his ex-wife's family. Fowzia has launched a 500m rupees (£360,000) defamation law suit, while regularly attacking him in the press as a wifebeater set on "destroying" her family. "Marrying him was Aafia's biggest mistake," she told me. Khan says it is a ploy to silence him in the media and take away his children.

Khan's explanation is bolstered by the one person who claims to have met the missing neuroscientist between 2003 and 2008 – her uncle, Shams ul-Hassan Faruqi. Back in Islamabad, I went to see him.

A sprightly old geologist, Faruqi works from a cramped office filled with coloured rocks and dusty computers. Over tea and biscuits he described a strange encounter with his niece in January 2008, six months before she was captured in Afghanistan.

It started, he said, when a white car carrying a burka-clad woman pulled up outside his gate. Beckoning him to approach, he recognised her by her voice. "Uncle, I am Aafia," he recalled her saying. But she refused to leave the car and insisted they move to the nearby Taj Mahal restaurant to talk. Amid whispers, her story tumbled out.

Siddiqui told him she had been in both Pakistani and American captivity since 2003, but was vague on the details. "I was in the cells but I don't know in which country, or which city. They kept shifting me," she said. Now she had been set free but remained under the thumb of intelligence officials based in Lahore. They had given her a mission: to infiltrate al-Qaida in Pakistan. But, Siddiqui told her uncle, she was afraid and wanted out. She begged him to smuggle her into Afghanistan into the hands of the Taliban. "That was her main point," he recalled. "She said: 'I will be safe with the Taliban.'"

That night, Siddiqui slept at a nearby guesthouse, and stayed with her uncle the next day. But she refused to remove her burka. Faruqi said he caught a glimpse of her just once, while eating, and thought her nose had been altered. "I asked her, 'Who did plastic surgery on your face?' She said, 'nobody'."

On the third day, Siddiqui vanished again.

Amid the blizzard of allegations about Siddiqui, the most crucial voice is yet to be heard – her own. The trial, due to start in January, has suffered numerous delays. The longest was due to a six-month psychiatric evaluation triggered by defence claims that Siddiqui was "going crazy" – prone to crying fits and hallucinations involving flying infants, dark angels and a dog in her cell. "She's in total psychic pain," said her lawyer, Dawn Cardi, claiming that she was unfit to stand trial.

But at the Texas medical centre where the tests took place, Siddiqui refused to co-operate. "I can't hear you. I'm not listening," she told one doctor, sitting on the floor with her fingers in her ears. Others reported that she refused to speak with Jews, that she manipulated health workers and perceived herself to "be a martyr rather than a prisoner". Last July three of four experts determined she was malingering – faking a psychiatric illness to avoid an undesirable outcome. "She is an intelligent and at times manipulative woman who showed goal-directed and rational thinking," reported Dr Sally Johnson.

Judge Richard Berman ruled that Siddiqui "may have some mental health issues" but was competent to stand trial.

Back in Pakistan Siddiqui has become a cause celebre. Newspapers write unquestioningly about her "torture", parliament has passed resolutions, placard-waving demonstrators pound the streets and the government is spending $2m on a top-flight defence. High-profile supporters include the former cricketer Imran Khan and the Taliban leader Hakumullah Mehsud who has affectionately described Siddiqui as a "sister in Islam".

The unquestioning support is a product of public fury at US-orchestrated "disappearances", of which there have been hundreds in Pakistan, and deep scepticism about the American account of her capture. Few Pakistanis believe a frail 5ft 3in, 40kg woman could disarm an American soldier; fewer still think she would be carrying bomb booklets, chemicals and target lists.

But there are critics, too, albeit silent ones. A Musharraf-era minister with previous oversight of Siddiqui's case told me it was "full of bullshit and lies".

Two weeks ago the Obama administration introduced a fresh twist, when it announced that next year (or in 2011) five Guantanamo Bay detainees will be tried in the same New York courthouse, a few blocks from the World Trade Centre. One of them is Siddiqui's second husband, Ammar al-Baluchi, also known as Ali Abd al-Aziz Ali, who stands accused of financing the 9/11 attacks.

But while the Guantanamo detainees will be tried for their part in mass terrorism, Siddiqui's case focuses on a minor controversy – whether she fired a gun at a soldier in an Afghan police station. And so the big questions may not be probed: whether the ISI or CIA abducted Siddiqui in 2003, what she did afterwards, and where her two missing children are now. In fact the framing of the charges raises a new question: if Siddiqui was such a dangerous terrorist five years ago, why is she not being charged as one now? A senior Pakistani official, speaking on condition of strict anonymity, offered a tantalising explanation.

In the world of counter-espionage, he said, someone like Siddiqui is an invaluable asset. And so, he speculated, sometime over the last five years she may have been "flipped" – turned against militant sympathisers – by Pakistani or American intelligence. "It's a very murky world," he said.

"Maybe the Americans have no charges against her. Maybe they don't want to compromise their sources of information. Or maybe they don't want to put that person out in the world again. The thing is, you'll never really know."