John Walker Lindh, the California man captured while fighting with the Taliban in Afghanistan, pleaded guilty to two charges Monday in a surprise deal that will spare him from spending the rest of his life in prison.

The unexpected deal caught even the trial judge off guard.

"I plead guilty. I plead guilty, sir," Walker, 21, told U.S. District Judge T.S. Ellis III early in what was supposed to be a weeklong series of hearings at which defense lawyers hoped to get statements Walker made to investigators thrown out of his trial.

Walker pleaded guilty to one charge of supplying services to the Taliban and another charge, not originally in the indictment, that he carried explosives in the commission of a felony.

Under terms of his deal with prosecutors, Walker, 21, will be out of jail before he turns 42. He has agreed to serve two 10-year prison sentences and will cooperate fully with U.S. authorities in their terror investigation.

Walker, who was captured in early December, had been charged with 10 federal crimes, including conspiracy to murder U.S. citizens, contributing services to Al Qaeda and the Taliban and using firearms during crimes of violence.

Three of the 10 counts could have carried maximum terms of life imprisonment for Walker, who was transferred to civilian custody in late January. His trial was set to begin on Aug. 26.

"The court finds your plea of guilty to be knowing and voluntary," Ellis told the defendant after his surprise plea. "The court accepts your plea and adjudges you now guilty."

With his parents and younger sister seated behind him, Walker rose in his green prison jumpsuit to face the judge and state in his own words the crimes he committed.

"I provided my services as a soldier to the Taliban last year from about August to November. During the course of doing so I carried a rifle and two grenades," he said.

U.S. District Attorney Paul J. McNulty, chief prosecutor in the case, called the agreement "an important victory for the American people in the battle against terrorism. This is a tough sentence. This is an appropriate punishment and this case proves that the criminal justice system can be an effective tool in the fight against terrorism."

The plea deal, McNulty said, means the U.S. government is now "able to use our limited and very vital resources, not only to continue to prosecute terrorists but to pursue the military campaign."

Noting Walker's ongoing cooperation, Attorney General John Ashcroft hailed the plea deal.

"By going to Afghanistan and fighting shoulder-to-shoulder alongside the Taliban, John Walker Lindh allied himself with terrorists who reject our values of freedom and democracy and turned his back on the United States of America," Ashcroft said. "He will now spend the next 20 years in prison — nearly as long as he has been alive."

President Bush approved the parameters of the deal last week. A White House official, speaking on condition on anonymity, said Justice Department lawyers informed the White House counsel's office on Wednesday about their intention to strike a deal with Walker, along with the outlines of the terms they would offer. Armed with the president's approval, the Justice Department negotiated the terms over the weekend.

At the Pentagon, spokeswoman Victoria Clarke said that as part of the deal, Walker agreed to "cooperate on any future intelligence and information gathering," with regard to his knowledge about the Taliban and Al Qaeda.

Shannon Spann, widow of Johnny "Mike" Spann, the CIA agent who interviewed Walker and was killed in a prison uprising before Walker's capture, told Fox News she had mixed feelings about the guilty plea.

"On a personal level I actually take a great deal of satisfaction from the plea," she said. "He acknowledged his conduct has contributed to Al Qaeda. He has in fact admitted he is a terrorist. . . .

"I don't believe that 20 years is  . . . I don't believe that a lifetime is . . . enough to compensate for thousands of Americans being killed," Spann said. But "on a personal level, I'm gratified that he has somehow found it within himself to agree that he was involved."

Spann added: "I’m also certainly pleased the government secured Walker’s full and complete cooperation. He has agreed to cooperate on any subject, and that is important."

Walker, who grew up in a middle class California family before journeying to Afghanistan, was slated for trial in late August.

His lawyers had planned to use a series of hearings this week to ask Ellis to throw out statements Walker made during his capture because he had not been advised of his rights.

The judge even opened the hearing Monday with some procedural remarks, before defense lawyer James Brosnahan interjected.

"There is a change in plea," Brosnahan said, explaining the deal was reached during negotiations Sunday night.

Walker's father said he was thankful the government dropped the more serious terrorism charges. Frank Lindh said he told his son after he was brought back to the United States that South African leader "Nelson Mandela served 26 years and I told him to be prepared for something like that."

"John has no bitterness," Frank Lindh added.

Walker's mother described him as a "kind, humble and a loving son" who went to Afghanistan to "satisfy a thirst" for Islam.

"He did not go to Afghanistan with the intention of fighting against the United States," Marilyn Walker said.

Before acepting the plea, Ellis asked Walker if he was willing to forego a trial.

"Yes, sir," Walker responded.

The judge then asked Walker a series of standard questions about his background.

"I attended some college in California as well as Yemen," Walker explained in a soft voice.

The judge asked him to speaker louder. "Do you feel as though you can make a decision about your future today?" Ellis asked.

"Yes," responded Walker, who will be 41 when he gets out of prison under terms of the plea deal.

Walker, from a middle-class family in Marin County, Calif., broke onto the American scene in December when he was discovered among Taliban prisoners captured in Afghanistan.

With long hair and a beard, he gave a hospital bed interview to a freelance TV reporter describing his allegiance to the Taliban.

In military interrogations, he also claimed to have met Usama bin Laden once, government lawyers said.

It was those statements that his lawyers were seeking to keep out of the trial before the deal was reached.

While Walker's team had disputed government accounts of his statements, prosecutors contended that he described enlisting in the Taliban; training at a camp the government says was run by Al Qaeda; meeting with bin Laden in Afghanistan in the summer of 2001; and learning from others at the camp that the Al Qaeda leader had sent operatives to carry out suicide missions against the United States and Israel.

In advance of Monday's announcement, Walker's lawyers had been plotting a strategy aimed at challenging the use in court of statements he made while still in Afghanistan. They had contended that the failure to tell him of his right to remain silent and have an attorney present violated his rights. They also had said that Walker was malnourished, deprived of sleep, bound and blindfolded, conditions that should invalidate anything he said.

Prosecutors responded that the Miranda rule spelling out a defendant's rights has no place on the battlefield. They argued Walker was treated as well as U.S. soldiers in the field.

The Associated Press contributed to this report.