A man who lost his life in a shooting at an illegal after-hours bar in Brooklyn was plugged accidentally by his friend and fellow gang member, who unloaded when they got in an argument with another group, according to police.

Gabriel Hernandez, 27, was arrested and charged with manslaughter and weapons possession Wednesday, two days after he allegedly fatally shot Giovanni Rivera, 31, in the back while the men were carousing inside a bar on 50th St. near Third Ave. in Sunset Park around 5 a.m.

Hernandez and Rivera, old pals and allegedly both members of La Raza Loca gang, were drinking inside the bar with the victim’s girlfriend when they got into an argument with a group of four or five men around 4:23 a.m., NYPD Chief of Detectives Joseph Kenny said at a press briefing Thursday.

After briefly disengaging, the groups got into it again. Hernandez, who is also known as “Popoca,” pulled out a pistol and tried to menace the other group, “and he lets two (shots) go, striking his own friend in the back, causing his demise,” Kenny said.

Gabriel Hernandez, 27, is facing manslaughter and weapon possession charges for allegedly shooting Giovanni Rivera in the back and chest inside the unlicensed spot on 50th St. near Third Ave. in Sunset Park about 5 a.m. Monday. (Google)Gabriel Hernandez, 27, is facing manslaughter and weapon-possession charges for allegedly shooting Giovanni Rivera in the back and chest inside an unlicensed nightspot on 50th St. near Third Ave. in Sunset Park around 5 a.m. Monday. (Google)

Rivera, who was also known as “Puppet” and “Problema,” was shot in the back, with the bullet then hitting his heart, said Kenny, who described the incident as “friendly fire” and “drunken tomfoolery.”

After the bullets flew, the other group ran off and a bar employee wrestled Hernandez to the ground and disarmed him, Kenny said.

When Hernandez saw his friend covered in blood, he tried to render aid. But when bar patrons and employees tried to help, too, Hernandez started beating them up, according to Kenny.

Everyone involved was “extremely intoxicated,” Kenny said.

Hernandez was arrested at the scene after failing to convince cops to give him his gun back, Kenny said.

Medics rushed Rivera to NYU Langone Hospital-Brooklyn where he died. He lived in Borough Park.

Gabriel Hernandez, 27, is facing manslaughter and weapon possession charges for allegedly shooting Giovanni Rivera in the back and chest inside the unlicensed spot on 50th St. near Third Ave. in Sunset Park about 5 a.m. Monday. (Google)Gabriel Hernandez, 27, is facing manslaughter and weapon-possession charges for allegedly shooting Giovanni Rivera in the back and chest inside an unlicensed nightspot on 50th St. near Third Ave. in Sunset Park about 5 a.m. Monday. (Google)

Hernandez has been arrested six times before, most recently for misdemeanor assault after punching a 28-year-old relative on Oct. 17, officials said.

He was released without bail and recommended to take part in an alternative-to-incarceration program focused on problem-solving and anger management, court documents show.

In May he got slapped with a weapons-possession charge after cops found a pistol in his waistband during a traffic stop. Both cases are still pending in court.

Rivera, cops said, also had six arrests under his belt. He and Hernandez were arrested together in 2016 for the nonfatal shooting of a friend, according to Kenny.

Rivera’s most recent arrest was on July 13 for allegedly attacking his ex-girlfriend and robbing her of her cell phone. He was released at his arraignment since the charges were not bail eligible, officials said.

That case was still pending in Brooklyn Criminal Court when he met his end.

Hernandez’s arraignment was pending on Friday.