After a daylong hearing, state District Judge Freddie Romero ordered the boy, now 13, held in state custody until he is 21.
Prosecutors had sought the maximum punishment for the boy, who pleaded no contest to the January shooting in Roswell. His lawyers asked that he be placed in treatment for two years and then released if doctors determine he is no longer a threat.
A defence memo said the boy was chronically bullied, is socially and emotionally immature, and regrets what he did.
Roswell police said the boy took a modified shotgun to Berrendo Middle School in southeastern New Mexico on Jan. 14 and opened fire on students as they waited for classes to begin.
Nathaniel Tavarez, 12, and Kendal Sanders, 13, were injured in the shooting, which sent the school into a panic. Tavarez spent weeks in hospitals and rehabilitation centres for treatment of wounds to his chest, heart, face and head. His vision in both eyes has been severely diminished.
The boy was charged with three counts of aggravated battery with a deadly weapon and one count of carrying a firearm on school premises. His lawyers have said his no-contest plea acknowledged the "limitations in decision-making of an immature brain."
Under New Mexico law, the state can charge minors as adults only if they are at least 14.
The Associated Press typically doesn't identify juveniles charged with crimes.