The assault, which also left 13 civilians wounded including the chief judge, comes a day after the Taliban killed more than a dozen people after pulling them off buses in the northern province of Kunduz.
30 dead, hundreds wounded as Taliban attack rattles Kabul
"Four attackers entered the court compound after killing the guard," provincial police chief Aminullah Amarkhail told AFP.
"One of the attackers detonated his suicide vest inside the compound and the three others were gunned down by police."
The interior ministry said five civilians and one policeman were killed in the siege which lasted around 30 minutes.
The Taliban claimed responsibility for the attack, which underscores Afghanistan's fragile security situation as the militants intensify assaults against government forces after launching their spring offensive in April.
In the Kunduz incident on Tuesday, the Taliban also kidnapped dozens of people after offloading them from four buses, officials said. Their fate is still not clear.
The insurgents claimed they were targeting Afghan security officials aboard the buses passing through the insurgency prone district of Aliabad.
Two charred bodies found in Balochistan near Pak-Afghan border
The Taliban last Wednesday announced Haibatullah Akhundzada as their new leader, elevating a low-profile religious figure in a swift power transition after officially confirming the death of Mullah Mansour in a US drone strike.
The drone attack, the first known American assault on a top Afghan Taliban leader on Pakistani soil.
Mansour was killed just nine months after being formally appointed leader following a bitter power struggle upon the confirmation of the death of Taliban founder Mullah Omar.
On Tuesday Pakistan said his body had been handed back to his family.
COMMENTS
Comments are moderated and generally will be posted if they are on-topic and not abusive.
For more information, please see our Comments FAQ