The raid was mounted in Kunduz, a city which the insurgents have encircled in recent weeks and attempted to storm, so far without success.
Four attackers wearing army uniforms attacked the provincial appeals court, triggering a four-hour gunbattle with Afghan security forces, provincial police spokesman Sayed Sarwar Hussaini told AFP.
“They first blew up an explosives-laden car at the gate of the court and then entered the building,” he said.
“The attackers killed six court officials and one police. Eight people were wounded,” he said, adding that the militants were also killed.
Chief prosecutor Amruddin Amin said the gunmen went door-to-door in the court compound, shooting their victims at close quarters.
“I jumped out of a window. I saw the gunmen shooting people at close range,” he said.
“The police guards inside the building fought them for hours, but they were eventually overpowered after they ran out of ammunition.”
The Taliban claimed responsibility for the attack on their website and said several court officials and prosecutors were killed.
They did not elaborate on the motive for the attack but the court was hearing several cases against Taliban militants.
A suicide car bomber rammed into the Supreme Court building in Kabul in June last year, killing 15 civilians and wounding 40 others including women and children.
The Taliban later claimed responsibility and threatened further attacks on the judiciary if it continued to sentence its members to death.
The insurgents also stormed a provincial court in the western town of Farah in April 2013, killing 44 people in a bid to free insurgents standing trial. The dead included 34 civilians, while all nine attackers were also killed.
The insurgents claimed that 13 of their prisoners used the opportunity to escape.
Kunduz province has seen a surge in fighting between the Taliban and Afghan forces, who will be left to defend their country without the aid of foreign combat troops by the end of the year.
Fighting has also picked up in the northern province of Faryab and in Logar province, which is close to the capital Kabul.
The last British combat troops formally pulled out of southern Afghanistan Sunday along with the last contingent of US Marines.
There are now about 40,000 foreign troops in Afghanistan, down from their 2011 peak of around 140,000.
But all NATO combat troops will depart the country by December.
A residual force of around 12,000 soldiers including 9,800 Americans and 500 Britons will remain as part of a security pact signed by new president Ashraf Ghani.
They will focus on training local forces and counter-terrorism.
The US military estimated this month that 7,000-9,000 Afghan police or troops have been killed or wounded so far this year. – AFP