"I can confirm that 34 civilians, six army and four policemen have been killed and 91 people, the majority of them civilians, have been injured," Najib Danish, interior ministry deputy spokesman said. "Nine attackers have also been killed."
It was not immediately clear whether the accused men had escaped the court complex in the western town of Farah, although a hospital doctor said one prisoner was among those being treated for injuries.
The death toll was the highest in Afghanistan from a single attack since a shrine was bombed in Kabul in December 2011, killing 80 people.
"The attack is over, but the casualties have unfortunately risen," Farah provincial governor Mohammad Akram Khpalwak said, putting the final death toll as high as 46.
"In total, 34 civilians and 12 security forces have been killed in the attack. We have also discovered the bodies of eight attackers, more than 100 people have also been injured."
Taliban militants fighting the central government claimed responsibility.
"Our fighters attacked several government buildings in Farah according to their planned tactic. They conducted the attack with small arms and grenades," the group said on its website.
"The fighting happened after information that (President Hamid) Karzai's administration wanted to try several fighters in a cruel way in this court."
Taliban fighters frequently target government compounds equipped with suicide vests, rockets and machine-guns.