Three militants threw hand grenades and opened fire as they launched the assault in Tangi, near the border with Afghanistan, on Tuesday morning.
Two of the men blew themselves up during the 20-minute gun battle with security forces at the gate, while the third was shot dead by police before he could detonate his explosive vest.
Ijaz Khan, chief of the district police, told Reuters: “The terrorists had come and wanted to kill as many people as they could inside the judicial complex.”
They were prevented from entering the building but killed five people in the assault – a child, a lawyer and four police officers – and injured dozens more.
Mohammad Shah Baz Khan described scenes of panic during the battle, saying several people scaled the complex’s walls in efforts to escape.
"Lawyers and other people in the complex started running to save their lives. There was panic and nobody knew where to go," he said.
Police had been on high alert after receiving intelligence that terror groups could target courts in Charsadda, Khyber Pakhtunkhwa province, following similar attacks elsewhere in Pakistan.
More than 100 people have been killed in a wave of atrocities that started last week, including an Isis suicide bombing that killed at least 90 people at a Sufi shrine on Friday.
Jamaat-ul-Ahrar, an affiliate of the Pakistani Taliban, has also launched numerous bombings and attacks after announcing the start of a new campaign of violence against the government, security forces, the judiciary and secular political parties.
The group claimed responsibility for the court attack via Taliban propaganda channels, issuing a statement in Urdu claiming “apostates were sent to hell by martyrdom fighters”.
Jamaat-ul-Ahrar published a photo of the two militants who detonated their bombs, who appeared to be teenage boys.
There were rumours the group had pledged allegiance to Isis after it split of the Taliban and expressed support for the so-called Islamic State, but the faction has now formally re-joined its parent group.
Pakistan's army chief General Qamar Javed Bajwa praised police for "saving many lives" in the group’s latest attack, according to the military statement.
Police Academy attack in Quetta, Pakistan
Police Academy attack in Quetta, Pakistan
Pakistani Army Major General Sher Afgun speaks to journalists as Balochistan's provincial interior minister Mir Sarfraz Bugti looks on after an attack by suspected militants at a police training centre in Quetta, Pakistan
Pakistani troops deploy outside the Police Training Centre after an attack on the center in Quetta, Pakistan
An ambulance carries injured officers to a hospital outside a police training school attacked by militants in Quetta, Pakistan
Medical workers treat an injured policeman at hospital after militants attacked the Balochistan Police College in Quetta
A police cadet from the Police Training Centre lies in the hospital after being injured after an attack on the center in Quetta, Pakistan
Relatives of police cadets wait for word outside the Police Training Center after an attack on the center in Quetta, Pakistan
Pakistani men take care of injured relatives at a hospital in Quetta
The death toll from an overnight attack on a police academy in southwest Pakistan has risen to 58 people with dozens more wounded
Pakistani soldiers pass through the entrance to The Police Training College in Quetta
Pakistani rescue workers transport an attack victim in Quetta
Pakistani security personnel stand guard as an ambulance carrying the coffins of attack victims drives past in Quetta
Zafar Iqbal Jhagra, the governor of Khyber Pakhtunkhwa province, told reporters that fresh talks with the militants cannot be ruled out.
"Talks can be held with everyone, including the Taliban," he added. "[But] we will not bow before the terrorists."
Isis’ shrine bombing, the deadliest terror attack in Pakistan for years, was followed by a nationwide crackdown by the military that resulted in the deaths of more than 100 alleged “terrorists” in 24 hours.
The operation sparked warnings from humanitarian groups for Pakistani authorities to adhere to international law.
The violence has also worsened relations between Afghanistan and Pakistan, who have accused each other of harbouring terrorists and each sent lists of named militants allegedly residing in the neighbouring country.
A number of Pakistani Taliban commanders have been killed in Afghanistan, while Afghan Taliban leader Mullah Akhtar Mansour and al-Qaeda leader Osama bin Laden were killed by US strikes while hiding in Pakistan.