Sheriff Tim Fulton said the boys told investigators they intended to harm the young girl because she had teased two of them.
The plot was uncovered late Wednesday morning, about a half-hour before recess, when another student alerted school officials,
"A young man — our hero here — came to the principal, Kelly Anderson, and informed her that he was aware of a gun being on the premises," School Superintendent Dave Shreeve told CBS affiliate KTVQ Billings.
The gun, a .22-caliber revolver, had two bullets in it, Michael Hayworth, the Rosebud County attorney, said. Shreeve said a box of bullets also was found nearby.
The boys were identified in court records as Klint Cook and Levi Strait, both 8, and Blake Belgarde, 11. They were charged Thursday in juvenile court with conspiracy to commit assault with a weapon.
"There was nothing that I would've had a sense that something like this could've been put together and occur," said Anderson, the principal.
"From the interviews (with investigators), I don't believe that they fully comprehended the full significance of their actions," Hayworth said. "But they understood that this was going to bring harm to her ... and they intended that."
In a court affidavit, Hayworth said Cook apparently brought the gun to school Wednesday morning and buried it in the sandbox, while Belgarde brought the paring knife from home. Authorities initially said the paring knife also was hidden in the sandbox, but Hayworth said in an affidavit that Strait had the knife in his possession when questioned by school officials.
"I need to commend that young man for such a brave act," Fulton said. "He saved the lives of who-knows-how-many people."
Forsyth is about 100 miles east of Billings. The elementary school has about 200 students.
Many of the parents arriving to pick up their children Thursday said they were not even aware of the incident until they heard news reports of it Thursday.
The school was sending letters home with students on Thursday, and had a counselor available during the day to talk to any students. The letter urged parents in this rural area to lock their firearms and store the bullets separately.
"Needless to say, it is a frightening experience for a community and our schools," Shreeve said. "We believe it's an isolated incident but it does bring into reality that this can happen in any place at any time."