Austin Wyatt Rollins, 17, shot a 16-year-old girl, who is in a critical condition, and a 14-year-old boy.
The attack happened on Tuesday morning before lessons at Great Mills High School in St Mary's County, 65 miles (104km) south-east of Washington DC. Rollins had a prior relationship with the girl, Sheriff Tim Cameron said.
The school shooting comes a month after a high school shooter killed 17 in Parkland, Florida. Blaine Gaskill, an armed campus guard, exchanged gunfire with the attacker but it is unclear if the gunman was hit by the guard's gunfire or shot himself.
A student is comforted Florida survivors: Don't arm teachers US students in mass walkout for gun reform Mr Cameron said the shooting began in a hallway at the school.
The gunman fired at a female and then a male student, hitting them both, he said.
"Our school resource officer who was stationed inside the school was alerted," he told reporters.
"He pursued the shooter, engaged the shooter.
During that engagement he fired a round at the shooter.
"Simultaneously, the shooter fired a round as well," he said.
Image ST MARY'S COUNTY resource officer Blaine Gaskill Image caption.
School resource officer Blaine Gaskill engaged the shooter Mr Gaskill was not hurt, Mr Cameron said, adding that the gunman's cause of death was still under investigation.
The female student is in a critical condition in hospital.
Thousands of students protest for gun reform Image caption .
Thousands of students protest for gun reform Some 1,600 students attend the school in the community of Great Mills near the Chesapeake Bay and were evacuated to a nearby school after the event.
Federal agents from the FBI, and the bureau of Alcohol, Tobacco, Firearms and Explosives - as well as local police - are at the school investigating the incident.
One Twitter user, who appears to be a student at the school, posted about the shootings.
Maryland Governor Larry Hogan has also expressed concern, saying "prayers are not enough".
"Today's horrible events should not be an excuse to pause our conversation about school safety," he said in a statement.
"Instead, it must serve as a call to action."
New York Governor Andrew Cuomo said the attack serves as "another tragic and exasperating reminder of the enduring threat of gun violence", and called upon "our so-called leaders in Washington" to take action to stem gun attacks.
The shooting comes four days before the March for Our Lives rally for student safety inspired by the 14 February massacre at Marjory Stoneman Douglas High School in southern Florida.