Media playback is unsupported on your device Media caption Footage from the scene shows the aftermath of the "raging battle" inside the bazaar, as the BBC's Caroline Wyatt reports

A suicide bomb and gun attack on a courthouse in western Afghanistan has left more than 50 people dead and 90 injured, most of them civilians.

Militants disguised as soldiers tried unsuccessfully to free suspected Taliban members in the capital of Farah province, Afghan officials said.

After a fierce initial gun battle, shooting continued as militants took cover in nearby buildings.

The Taliban said they were behind the attack in Farah, which borders Iran.

This was the deadliest attack in Afghanistan since December 2011 when at least 70 people were killed in a blast at a Shia shrine in Kabul.

A bomb and gun attack on the Farah governor's compound last May left six policemen, a civilian and four attackers dead.

'Suicide belts'

Analysis Afghan officials say insurgents - working with drug dealers - want to destabilise western Afghanistan by launching attacks in Farah. The vulnerable province lies on the border with Iran and is a major route for drug and human smugglers. The strategic Herat-Kandahar-Herat highway and the ring road goes through Farah. Wednesday's attack is also a major intelligence failure. Using Afghan army uniforms and vehicles, the insurgents managed to pass through several checkpoints set up to prevent attacks against government officials. The attack will further dent the confidence of the Afghan people in the ability of the national army to protect them as foreign troops are preparing to withdraw in 2014.

The assault began at approximately 09:00 local time (04:30 GMT) on Wednesday in the city of Farah, the Afghan officials said.

The militants blew up an army vehicle packed with explosives, damaging nearby buildings - including the local governor's office - and two banks packed with civilians.

The gun battle then broke out with Afghan security forces, as militants took up positions in a number of buildings.

Initial reports suggested the target was the governor's compound before officials, and a Taliban spokesman, confirmed the attack had focused on the courthouse.

Provincial police chief Afgha Noor Kemtoz told the AP news agency that six attackers wore suicide bomb vests.

Mr Kemtoz later said that the stand-off was over.

Afghan officials said that 34 civilians, 10 members of the security forces and nine militants died.

According to the police chief, the aim of the attack had been to free 15 Taliban prisoners who were being transferred to the courthouse for trial.

"Definitely the plan was to free the prisoners with this attack but, thank God, they did not succeed," he added. "All the prisoners are accounted for."

I heard a huge bang and when I looked I saw several attackers armed with light and heavy weapons running towards the bank and the court Sayedo, Shopkeeper in Farah

However, Taliban spokesman Qari Yusuf Ahmadi was quoted by Reuters news agency as saying all the prisoners had been freed.

Following the blast, gunmen entered several "government institutions", he was quoted as saying by the Pakistan-based Afghan Islamic Press news agency.

This is a major failure and an embarrassment for the Afghan intelligence service, the NDS, and the government, the BBC's Bilal Sarwary in Kabul reports.

He says the attackers managed to drive through several checkposts set up few weeks ago to protect the provincial capital and key government offices. Maps pointing specific locations were also found in the pockets of several of the dead suicide attackers.

This suggests that the insurgents had been gathering intelligence and choosing targets well in advance, local officials told the BBC.

'Packed with civilians'

The majority of those wounded in the attack were civilians, a doctor at the local hospital told the BBC.

Local shopkeeper Sayed Jan told the BBC: "I heard a huge bang and when I looked I saw several attackers armed with light and heavy weapons running towards the bank and the court.

"There is blood and broken glass. There is a heavy exchange of gunfire and my shop has become a centre of the fighting."

Eyewitnesses, including doctors in the city, said both sides had used machine-guns and grenades.

Speaking to the BBC, a senior Afghan security source said the courthouse and nearby banks had been "packed with civilians" at the time of the attack.