The very short answer is that the (would-be) gunman was still holding the gun when Dent punched him. Dent grabs the gun and points it toward the floor, but it's not until he hits him that he appears to let go.
As such, Harvey's actions would be well within the scope of acting in self-defence and the defence of others within the court.
The script direction agrees. Dent grabs the gun but there's no indication that the mobster had let go of it.
ROSSI: Hostile? I’ll show you hostile.
Rossi jumps up, points a gun at Dent’s face. Screams from the gallery. Rossi pulls the trigger – the gun misfires with a pop. Dent steps forward, grabs the gun, decks Rossi with a right cross, unloads the gun and sets it down in front of Maroni.
The Dark Knight Trilogy: The Complete Screenplay
The novelisation is pretty unequivocal. Grabbing the gun and punching Rossi happened effectively simultaneously.
Rossi lifted his hand from his side, and somehow it was holding a gun.
He aimed at Dent’s face, barely four feet away, and pulled the
trigger. There was a click as the gun’s hammer fell on the firing pin,
but there was no shot. Dent took a single step forward, grabbing the
gun with his left hand as his right, curled into a fist, struck Rossi
in the mouth. Rossi slumped back into the witness chair and spat
blood.
Dent ejected the clip from Rossi’s weapon, letting it fall to the
floor, and crossed to where Maroni sat. He dropped the empty gun onto
the table in front of Maroni, and said, casually, “Ceramic .28
caliber. That’s how it beat the metal detectors. Made in China, I
believe.” Turning back to the witness box, he said, “Mr. Rossi, I
recommend you buy American.”
The Dark Knight: Official Novelisation
You may also wish to note that the quote addresses the issue (with some slightly altered dialogue) of how he got the gun into the courtroom