Image copyright AP Image caption Mr Roof was arrested the day after the shooting more than 200 miles away in North Carolina

The man accused of killing nine black churchgoers in South Carolina last month was able to buy a gun due to a background check failure, the FBI says.

Dylann Roof should have been stopped from purchasing a weapon due to a felony charge, FBI chief James Comey told reporters on Friday.

But he said the charge was either incorrectly entered into a background check system or mishandled by analysts.

Roof is facing nine counts of murder over the 17 June attack in Charleston.

Speaking to reporters at FBI headquarters in Washington DC, Mr Comey outlined a series of missed opportunities and incomplete paperwork that allowed Mr Roof to buy a firearm.

Mr Roof, 21, was charged with possessing drugs just weeks before the attack on the Emanuel AME Church, and police said he admitted to the offence.

That admission should have been enough to stop him from buying a weapon, Mr Comey said, but the offence was incorrectly added to Mr Roof's record.

This meant the FBI analyst doing the mandatory background check on Mr Roof did not see it. "If she had seen that police report," Mr Comey said, "that purchase would have been denied."

'We are all sick'

He said he learned about the problem on Thursday night and FBI officials were meeting with relatives of the nine victims on Friday.

An internal review into how the agency uses criminal background checks in gun transactions has been launched. The FBI runs background checks for gun dealers in about 30 states, including South Carolina.

"We are all sick that this happened. We wish we could turn back time," he added.

Image caption An image of a gun that was posted on a website believed to have been owned by Dylann Roof

Mr Comey's comments came on the same day that South Carolina removed the Confederate flag from the capitol grounds in a ceremony attended by some relatives of the church shooting victims.

The flag was the battle emblem of southern states in the US Civil War and was raised over South Carolina's statehouse in 1961 to mark the 100th anniversary of the conflict.

Critics have long called it a symbol of slavery and the backlash against it grew when pictures of Mr Roof posing with the banner were discovered online.

Mr Roof was arrested the day after the shooting more than 200 miles away in North Carolina and then flown back to Charleston.

He appeared in court via a video link for a bail hearing last month and is next expected in court in October.

What stops you from buying a gun in the US

having served a prison term of more than a year

being the subject of an active felony or misdemeanour warrant

a conviction for using or possessing drugs in the last year

multiple arrests for using or possessing drugs within the last five years, with the most recent in the last 12 months

being in the US illegally

being dishonourably discharged from the US armed forces

renouncing your US citizenship

Source: FBI fact sheet