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 capital of the 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, the BBC's Caroline Wyatt in Kabul reports.
A bomb and gun attack on the governor's compound last May left six policemen, a civilian and four attackers dead.
'Suicide belts'
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 set off a large army vehicles packed with explosives, damaging nearby buildings - including the local governor's office - and two banks packed with civilians.
Scenes from attack on courthouse in Farah province which killed more than 50 and injured more than 90 people on April 3, 2013. (Photos: BBC & Reuters)
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."
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.