WASHINGTON The man accused of killing nine people in a historically black church in South Carolina last month was able to buy the gun used in the attack because of a breakdown in the federal gun background check system, the FBI said Friday.

Despite having previously admitted to drug possession, the man, Dylann Roof, 21, was allowed to buy the .45-caliber handgun because of mistakes by FBI agents, a failure by local prosecutors to respond to a bureau request for more information about his case, and a weakness in federal gun laws.

“We are all sick this happened,” said James B. Comey, the FBI director. “We wish we could turn back time. From this vantage point, everything seems obvious.”

The authorities’ inability to prevent Roof from obtaining the weapon highlighted the continuing problems in the background check system, which was designed to keep guns out of the hands of criminals, drug users and mentally-ill people. Despite new procedures and billions of dollars that have been spent on computer upgrades in the years after the September 11, 2001, terrorist attacks, federal authorities still do not have a seamless way of examining Americans’ criminal histories that eliminates human error.

The disclosure also introduced another element of politics into the aftermath of the massacre, which has already led lawmakers in South Carolina to remove the Confederate battle flag that flew outside its State House. Republicans and Democrats quickly seized on the background check failure as the latest evidence to back up their views on gun laws.

Roof exploited the three-day waiting time that has allowed thousands of prohibited buyers to legally purchase firearms over the past decade — and some of those weapons were ultimately used in crimes, according to court records and government documents.

The Department of Justice’s inspector general has been investigating the three-day loophole for some time, Comey said.

In an hourlong briefing with reporters, Comey said the FBI had begun informing the victims’ family members about the breakdown. He also said that he had ordered a review of the episode and that its findings be reported to him within 30 days.

According to Comey, Roof first tried to buy the gun on April 11 from a dealer in West Columbia, South Carolina. The FBI, which operates the National Instant Criminal Background Check System, received a call from the dealer, seeking approval to sell Roof the weapon. The FBI did not give the dealer the authority to proceed with the purchase because the bureau said it needed to do more investigating of Roof’s criminal history, which showed he had recently been arrested.

Under federal law, the FBI has three business days to determine whether there is sufficient evidence to deny a purchase. If the bureau cannot come up with an answer, the purchaser can return to the dealer on the fourth day and buy the gun.

Many major gun retailers, like Wal-Mart, will not sell a weapon if they do not have an answer from the FBI, because of the fear of public criticism if the gun is used in a crime. The marginal sale of one gun means little to the bottom line of a large dealer, which is not the case for smaller stores like the one that sold Roof his gun.

Two days after Roof tried to buy the weapon, an examiner at the FBI’s national background check center in Clarksburg, West Virginia, began investigating his criminal history. The examiner found that Roof had been arrested this year on a felony drug charge, but not convicted. The charge alone would not have prevented him from buying the gun under federal law. But evidence that Roof had been convicted of a felony or was a drug addict would have resulted in a denial, so she continued to probe his background.

Because Roof had been arrested in a small part of Columbia in Lexington County and not Richland County, where most of the city is, the examiner was confused about which police department to call. She ultimately did not find the right department and failed to obtain the police report. Had the examiner gained access to the police report, she would have seen that Roof had admitted to having been in possession of a controlled substance and she would have issued a denial.

The examiner, however, did send a request to the Lexington County prosecutor’s office, which had charged him, inquiring about the case. The prosecutor’s office, however, did not respond.

Around that time the three-day waiting period expired, and Roof returned to the store and purchased the gun.

Comey said that he had spoken with the examiner, who he said had been working in that position for several years, adding that she was “heart-broken.”

Shortly after the details of the gun purchase were revealed, Republicans and Democrats on Capitol Hill, gun control advocates and Second Amendment defenders began wading into the matter.

The chairman of the Senate Judiciary Committee, Charles E. Grassley, R-Iowa, said the background check mistakes should not be used as an excuse to pass tougher gun laws.

“It’s disastrous that this bureaucratic mistake prevented existing laws from working and blocking an illegal gun sale,” Grassley said. “The facts undercut attempts to use the tragedy to enact unnecessary gun laws. The American people, and especially the victims’ families, deserve better.”

The ranking Democratic member on the committee, Patrick J. Leahy of Vermont, said: “We simply cannot have such failures in our background check system, and peoples’ lives are at stake. Clearly, more oversight is needed.”

Leahy said he expected the committee “will be looking further into this matter.”

Dan Gross, the head of the Brady Campaign to Prevent Gun Violence, said the disclosure “underscores the urgency of the message that Charleston families and the Brady Campaign took to Capitol Hill this week” for Congress to vote on a bill that would provide $400 million to enter the records of prohibited people into the FBI’s background check database.

“Brady background checks have been incredibly effective and have saved lives by blocking more than 2.4 million gun sales — more than 350 every day — to people we all agree shouldn’t have them, like domestic abusers, felons and other dangerous people,” Gross said.

Many of the deadliest shootings in the past decade have highlighted problems in the FBI’s background check system.

After a 2007 shooting in which 33 people died at Virginia Tech University, investigators discovered that the gunman, Seung-Hui Cho, also should not have been able to buy a gun because a court had previously declared him to be a danger to himself. The shooting led to legislation aimed at improving the background check system.

— New York Times News Service