The 16-year-old, who said he acted unknowingly, accepted the prosecutor's request for a six-year term in a juvenile detention center, followed by five years of probation.
The trial had been scheduled to last three days at the National Court, but the boy pleaded guilty right after the charges from prosecutor Blanca Rodriguez were read out by a court clerk.
The boy — shielded behind a screen — was asked if he understood the charges, confessed to them and accepted the prosecutor's jail-term request. All three times he answered simply, "Yes."
Thekilled 191 people and was blamed on Muslim militants linked to al Qaeda.
Amid a frenzy of media coverage, the boy went into the courtroom accompanied by his mother and a court-appointed lawyer. The youth's identity was protected under Spanish law.
The boy was initially facing a jail term of eight years. But the prosecutor lowered it to six because Spanish law stipulates that after age 23 a juvenile suspect has to go into an adult jail, and counselors said this would have harmed the boy accused in this case.
Prosecutors say the youth collaborated with a gang in northern Spain that sold the dynamite used in the backpack bombs planted on four trains on March 11.
In exchange for an undisclosed amount of money, the teenager allegedly took a Madrid-bound bus from the northern city of Oviedo and carried with him a gym bag sealed with a padlock and containing 33 to 44 pounds of dynamite stolen from a mine.
The boy has said he did not know what was in the bag.
He is then said to have delivered it to a Moroccan named Jamal Ahmidam, the alleged buyer of the dynamite.
Authorities say that altogether an estimated 440 pounds of dynamite exploded on the trains.
The boy was charged with possessing explosives for purposes of terrorism.
Ahmidam was one of seven suspects in the attacks who blew themselves up in an apartment outside Madrid on April 3 as police prepared to arrest them.
Sixteen adults have been jailed over the March 11 attacks on preliminary charges of terrorism or mass murder. But this stops short of a formal indictment.
The teenager was brought to trial quickly because under Spanish law a juvenile suspect cannot be held for more than six months without being tried. The boy was jailed on June 16.