The officer, who was not injured, was hailed a hero for his quick response protecting students and staff who had gathered at Dixon High School for a graduation rehearsal.
"He saved an enormous amount of lives," Lee County Sheriff John Simonton. His comments were echoed by Illinois Gov. Bruce Rauner, who in a statement credited "school resource officer Mark Dallas for his bravery and quick action to immediately diffuse a dangerous situation."
The teen gunman, whose name has not been released, suffered non-life threatening injuries.
The shooting happened shortly after 8 a.m. Police Chief Steven Howell, Jr., told reporters that the officer called to report that he'd spotted an armed male at the school. He said the officer confronted the former student near the school's west gym and as ran after him, the suspect fired several shots at the officer.
Howell said the officer returned fire, shot the suspect and took him into custody. Neither the officer nor anyone else at the school was injured. Police said they believe the gunman acted alone and that there was no further threat to anyone in the area. Howell declined to discuss why the former student brought a gun to the school.
When police searched the school they found that the faculty and students had barricaded themselves inside by blocking the classroom doorways with desks, chairs and other furniture — just as they had been trained to do.
"A lot of things went right today and many things could have gone wrong," Dixon Mayor Liandro Arellano Jr., told reporters at a news conference outside the school.
Officials said all schools in the city about 80 miles west of Chicago were placed on lockdown in the immediate aftermath of the shooting. The other schools re-opened after officials determined the gunman acted alone.