(CNN) The 21-year-old man who shot and killed two students at Aztec High School in New Mexico on Thursday had previously been investigated by the FBI for online comments about planning a mass shooting.

San Juan County Sheriff Ken Christesen on Friday identified the gunman as William Atchison. Christesen said at a news conference that the shooting was a "planned event" and that Atchison purchased a 9mm Glock last month, which he used in the shooting.

Atchison was a former student at the high school, but he didn't graduate, according to the FBI.

Casey Jordan Marquez and Francisco Fernandez, both 17, were killed in the shooting. Atchison died of what police believe to be a self-inflicted gunshot wound, New Mexico State Police Chief Pete Kassetas said. There were no other injuries.

FBI agent Terry Wade said the FBI had contacted Atchison in March 2016 after he made notable comments on an online gaming forum.

Wade did not specify what Atchison wrote, but said it was something along the lines of: "If you're going to commit a mass shooting, does anyone know about cheap assault rifles?"

Wade said the FBI interviewed Atchison and his family but it closed the case because the man did not have a gun and did not commit a crime.

Aztec is a city of about 6,000 people in northwest New Mexico, according to the US Census Bureau , and is about a three-hour drive from Albuquerque. The high school remained closed Friday, and investigators were treating it as a crime scene and were still trying to assess all the bullet holes. The high school has approximately 900 students.

How the shooting unfolded

Kassetas said Atchison entered Aztec High School at 8:04 a.m. on Thursday disguised as a student. He had a backpack with him in which he had his gun and multiple magazines, ammunition storage and feeding devices.

Atchison went to a second-floor bathroom, where he was "preparing to confront students." Classes were in session at the time. Kassetas said Francisco Fernandez entered the bathroom where Atchison was gearing up. The police chief said Atchison immediately fired his gun, fatally wounding the teen.

Officials identified the shooter as 21-year-old William Atchinson.

Atchison then walked out to the hallway and encountered Casey Jordan Marquez, and he "immediately shot her dead," Kassetas said.

The shooting could have been even deadlier if not for substitute teacher Kathleen Potter, who was with her students inside a computer lab, officials said. Christesen said Potter did not have the keys to lock the classroom because she is a substitute teacher, so she barricaded her students in the back of the classroom with a couch.

Kassetas said Atchison entered that room and knew there were students in the back. He fired multiple rounds to the wall, but did not hit anyone.

The suspect exited the computer lab into the hallway, at which point police believe he took his own life, Kassetas said.

Christesen praised Thomas Hill, the high school custodian. Hill heard the shots and followed Atchison, screaming at him and at teachers that there was an active shooter on site.

Christesen said law enforcement found a thumb drive on Atchison's body that contained several documents that appeared to reference the shooting. A message was written less than two hours before the incident.

"If things go according to plan, today would be when I die. I waited till the school buses are detected then head out on foot disguised as a student. I go somewhere and gear up, then hold a class hostage, then go ape****, then blow my brains out."

Kassetas said Aztec police shot out a window to gain access to the school. He said he believes that occurred just before Atchison took his own life.

'Parent's biggest nightmare'

Two students were killed at Aztec High School in New Mexico on Thursday.

Garrett Parker, a sophomore at Aztec High School, told CNN he was in a classroom when he and other students heard what they thought was someone punching lockers. As the noise got louder, they realized it was gunshots.

The students all had to hide until they were told by officials to walk out of the room toward the parking lot, he said.

"I just can't believe this happened in our community," said Garrett's mother, Sabrina Montoya. "This is all a horrible feeling. I'm glad my kids are safe and with us but I am devastated for my community and for the families who didn't get the chance to take their kids home."

Gov. Susana Martinez said there were several acts of bravery from staff, teachers, students and police that saved lives and prevented the incident from becoming deadlier.

"All New Mexicans are with Aztec today," she said.

US Sen. Martin Heinrich, a Democrat, said he was distraught to learn of the shooting and called for action to address the "epidemic of gun violence" in America.

"This is a parent's biggest nightmare. Every child deserves to be safe at school," he said.