The handgun that a 12-year-old middle school girl brought into her classroom on Thursday went off accidentally from inside her backpack, police said Friday.

It’s not yet clear how the weapon actually discharged.

The episode occurred at Salvador B. Castro Middle School in Los Angeles and the suspect – who has not been named – was taken into custody minutes later.

Investigators are working to determine how the girl got hold of the semi-automatic handgun and why she brought it with her to school, Los Angeles police spokesman Josh Rubenstein said.

LOS ANGELES SCHOOL SHOOTING SUSPECT ALLEGEDLY THOUGHT GUN WAS A TOY

Earlier Friday it was reported that the girl allegedly thought that the gun was a toy.

Jordan Valenzuela, 12, one of the suspect’s classmates, said the girl was crying after the gun went off and insisted it was an accident.

"She was like: 'I didn't mean to. I had the gun in my backpack and I didn't know it was loaded and my backpack fell and the gun went off,'" Valenzuela told The Associated Press. He added that he saw a hole in her backpack, which she was holding.

The suspect reportedly asked him to hide the gun for her but he said he refused. "Then I moved away from her because I was a little bit scared."

RECENT SCHOOL SHOOTINS IN THE US

One bullet hit a girl in the wrist before hitting a boy in the head, police said. Both victims are 15 years old.

The boy was initially listed in critical condition but doctors said that the bullet didn’t hit anything vital or cause any life-threatening injury. The girl’s wrist wound was considered minor. Three other people had minor face or head injuries, some from broken glass, but they weren’t shot, officials said.

The suspect is likely to face arraignment on Monday on two felony charges, which include being a minor in possession of a firearm and having a weapon on school grounds, prosecutors said.

The Associated Press contributed to this report.