He had been facing charges for allegedly claiming to be a prophet.
Blasphemy is legally punishable by death. No-one has been executed for it by the state but accusations can often lead to violent attacks.
The victim, Tahir Ahmad Naseem, was accused of blasphemy in 2018 by a teenager.
He was killed at a trial hearing on Wednesday morning. Video shared on social media shows his body slumped over the court's seats.
His attacker was arrested at the scene. Another video shows him in handcuffs, shouting angrily that his victim was an "enemy of Islam".