A wild scene erupted in a Brooklyn courthouse Tuesday when a reputed gang member charged in a grisly sex attack on a 12-year-old girl loudly cursed out the prosecutor, leading to a scuffle in the hallway afterward.

Tony Johnson, 17, was about to be returned to a holding cell when he suddenly started screaming at Brooklyn Assistant District Attorney Michele Haddad.

“F–king bitch! F–king bitch!,” Johnson yelled.

Haddad froze wide-eyed in shock as officers grabbed the handcuffed, 6-foot-1 teen by his upper arms and hustled him out of the courtroom.

Several of Johnson’s family members burst into tears and began sobbing in the gallery, then got up from their seats and headed to the door.

A teenage girl shouted “F–k you!” on her way out and lunged at a news photographer in the hallway, screaming that she didn’t want her picture taken.

A court officer grabbed the girl, forced her to the floor and kept her pinned down until she stopped ranting and composed herself.

The group was then led off by a woman who works in the courthouse.

The ruckus came just hours after The Post exclusively revealed how Johnson, a reputed member of the Folk Nation gang, was freed on $6,000 bail in March despite a prosecution request that he be held on $225,000 over his indictment on 34 counts of armed robbery, assault and other charges.

Following his release, Johnson, who lives in the Breukelyn Houses in Canarsie, allegedly went on another robbery spree that included an Oct. 8 attack on a 12-year-old girl who was walking through the sprawling housing project to pick up her younger sister from school.

After rifling through the girl’s bookbag and finding nothing of value, Johnson allegedly forced her into a basement, where he sexually assaulted her and forced her to engage in a sex act, court papers say.

The judge who granted Johnson the greatly reduced bail, acting state Supreme Court Justice Craig Walker, also presided over Johnson’s Tuesday arraignment in the sex assault, but made no mention of the earlier case.

During the proceeding, Johnson was assigned a new court-appointed defense lawyer, Laurence Rothstein, who entered not guilty pleas on Johnson’s behalf to charges that include first-degree criminal sexual act, first-degree sexual abuse and endangering the welfare of a child.

Rothstein did not challenge the $75,000 cash bail or $100,000 bond set following Johnson’s arrest on Thursday.

Johnson is also being held on another $30,000 in connection with three unrelated robberies in September.