Get breaking news alerts and special reports. The news and stories that matter, delivered weekday mornings.

Two teens at a Maryland high school were shot Tuesday after another student opened fire in the hallway just before the start of classes, authorities said.

The shooter — identified as Austin Wyatt Rollins, 17 — was injured and taken to the hospital, where he died, St. Mary's County Sheriff Tim Cameron said at a news conference.

Great Mills High School remained on lockdown for some time after, but the shooting was "contained" when a school resource officer "engaged" the gunman, Cameron said.

The chaos unfolded at 7:55 a.m. ET when police say Rollins walked into the 1,600-student school and fired a round from a Glock semiautomatic handgun at a 16-year-old female student. A 14-year-old male was also struck.

The school resource officer — St. Mary's County sheriff's Deputy Blaine Gaskill — was alerted to the gunfire and confronted the gunman in less than a minute, according to Cameron.

Gaskill "fired at the shooter almost simultaneously as the shooter fired," Cameron said, without detailing how the gunman was injured. The officer was not wounded.

"This is your worst nightmare," Cameron added.

He said Rollins and the female victim had a "prior relationship," but couldn't confirm if that was a motive for the shooting.

Let our news meet your inbox. The news and stories that matters, delivered weekday mornings. This site is protected by recaptcha

The female victim was hospitalized and listed in critical condition, according to MedStar St. Mary's Hospital. The male victim was in "good condition," the hospital said.

Students from Great Mills High School walk to meet their parents following the shooting Tuesday. Win McNamee / Getty Images

Agents with the Bureau of Alcohol, Tobacco, Firearms and Explosives also responded to the scene, which is around 70 miles south of Washington.

All high schools in St. Mary's County are staffed with an armed deputy sheriff who works as a school resource officer. Gaskill is also a trained member of the sheriff's SWAT team, officials said.

Students at the high school had participated in last week's walkout that honored the 17 victims of the mass shooting in Parkland, Florida.

Maryland Gov. Larry Hogan praised the officer's actions, but said legislation is needed to curb the availability of guns from certain people, including those with a mental illness.

"We need more than prayers. We've got to take action," Hogan told reporters.

The ATF is running a trace on the gun that Rollins used.

Students, meanwhile, were taken by bus after the shooting to a nearby high school to be reunited with panicked parents.

Terrence Rhames, a senior at Great Mills, said he and his friends were talking before class when they heard the pop of a loud gunshot.

"I heard one shot and when we ran, we saw a teacher and he was looking at us with a confused look," Rhames said. "We were trying to figure out if this is real."

He said students raced for the exits and called their parents.

"It was just shocking. You hear about shootings — you never think [they] would happen to you," Rhames said.

The incident came ahead of Saturday's national March For Our Lives rally against gun violence that follows the Parkland massacre.

Emma González, a Parkland senior and activist, tweeted her support to Great Mills students on Tuesday morning.

We are Here for you, students of Great Mills 💗 together we can stop this from ever happening again https://t.co/bkzL1FcVgu — Emma González (@Emma4Change) March 20, 2018

House Minority Whip Steny Hoyer, D-Md., told NBC Washington that he was "sickened" to hear of another school shooting, this time in his district.

Across the country, "we've had 16 school shootings since the beginning of the year, just about one a week," Hoyer said. "It's unacceptable. We need to take action."

The school and college shootings have included several which took place during school hours and resulted in injuries or deaths, but some have also involved unintentional gunfire or occurred when the school was closed.

The White House this month said it will propose funding firearms training for school personnel and bolster firearm background checks, but steered clear of a previous idea floated by President Donald Trump to raise the minimum legal age to buy semiautomatic weapons from 18 to 21.

The state of Maryland prohibits anyone under the age of 21 from having certain firearms, such as handguns or assault weapons.