The youth accompanied by another accused was being taken to court lockup after being produced before Additional District and Session Judge Sandeep Garg when an unidentified man shot at him, DSP Amrik Singh said.
The attacker fled away from the spot but left behind his pistol, he added.
The youth was rushed to a local hospital where he was referred to PGI, Rohtak considering his serious condition.
Police are investigating into the matter and a search for the attacker is in progress, Singh said.