The incident -- unrelated to the White House security breach -- took place in the lobby of the county justice center in West Chester, Pennsylvania, the local district attorney said.
The assailant, Curtis Smith from Coatesville, "pulled out a knife and attacked a deputy sheriff, slashing him," district attorney Tom Hogan told reporters.
"Another deputy sheriff immediately pulled his firearm and shot Smith," he added. Smith was treated at the scene but later died in hospital.
"Unfortunately because of incidents like this across the nation we have learned how to deal with an attack at the court house. Nobody else was injured, the sheriffs did their job," Hogan said.
Smith was arrested in March for allegedly climbing a perimeter wall at the White House, one of a string of recent security breaches at the residence of the US president in Washington.