WASHINGTON—Federal investigators should have prevented the accused South Carolina mass shooter from purchasing a handgun in April, FBI director James Comey said Friday.

An error made during a background check failed to disclose all details of the alleged assailant’s criminal background, which would have disqualified him.

The agency’s admission that an error in the gun-background check program played a role in the deaths of nine people at a Charleston church last month came on the day that South Carolina removed the Confederate battle flag from its Statehouse grounds, a move made in response to furor stemming from the alleged shooter’s affinity with the symbolic banner.

Mr. Comey in a meeting with reporters acknowledged the seriousness of the oversight, which occurred under the nation’s Brady Handgun Violence Prevention Act of 1993. He said he has launched an internal review to determine whether FBI officials should overhaul their firearm-review process to prevent such lapses in the future.

“It rips all of our hearts out,” Mr. Comey said. “The thought that an error on our part is connected to this guy’s purchase of a gun that he used to slaughter these people is very painful for us.”