U.S. prosecutors unsealed charges Tuesday against 48 defendants for participating in what investigators said was the largest ever prescription drug scheme that involved illegally trafficking pills for diseases such as HIV and AIDS to the black market.

The scheme, which caused those same drugs to ultimately be resold back into the legal distribution chain, led to the loss of more than $500 million in Medicaid reimbursements, federal officials said.

According to emails and documents gathered on warrants by investigators, defendants bought and sold more than $62 million worth of second-hand drugs during a 12-month period.

During the investigation, the Federal Bureau of Investigation seized more than $16 million worth of prescriptions drugs equivalent to more than 33,000 bottles and 250,000 loose pills.

Manhattan U.S. Attorney Preet Bharara said the Medicaid fraud was of “gigantic proportions.”

The prescription drugs for illnesses such as HIV, schizophrenia and asthma were originally dispensed to Medicaid recipients in the New York City area who then sold them into collection and distribution channels that ultimately ended at pharmacies.

The Medicaid beneficiaries were typically AIDS patients or people who suffered from other illnesses that required expensive drug therapies, in some cases costing more than $1000 per bottle. While Medicaid benefits would cover the costs for the pills, these patients would fill prescriptions at pharmacies and sell them to “collectors” – often on street corners and bodegas in New York neighborhoods such as Washington Heights or the Bronx - for cash, rather than using them for treatment.

The so-called collectors then sold the second-hand bottles to higher level participants in the scheme, which the FBI dubbed “aggregators,” who typically bought large quantities of drugs. These types of transactions continued up the food chain until pills were sold to wholesale prescription drug distribution companies, which then sold them back to pharmacies or other wholesale drug companies across the U.S.

The circulation would sometimes lead Medicaid to reimburse twice for the same set of pills. Conspirators profited by exploiting the difference between the cost to the patient of obtaining the prescription drugs through Medicaid (often free) and the hundreds of dollars per bottle.

“The scheme was theft, plain and simple, from a program funded by taxpayers,” FBI Assistant Director-in-Charge Janice Fedarcyk said in a statement.

The charges come three weeks after the U.S. Supreme Court upheld President Barack Obama’s healthcare reform law that calls for a major expansion to Medicaid. Individual states have the option to opt out of the expansion and several have already announced their intention to do so.

Shares of major drug companies like Pfizer (PFE) and AstraZeneca (AZN) followed the broader market higher on Tuesday afternoon, as did health insurers like WellPoint (WLP), UnitedHealth (UNH) and Aetna (AET).

In addition to the legal implications, Fedarcyk said the scheme posed “serious health risks.”

Because the drugs were ultimately resold into legal drug distribution channels, conspirators would repackage pills in bottles so as to make them appear new, directly from the manufacturer. They would restore bottles to their original appearance - with the manufacturer’s label intact but the patient labels removed – by using lighter fluid to dissolve the adhesive. If manufacturer labels were damaged during that process or if the pills were near expiration, some schemers would use counterfeit labels or alter them to backdate expiration dates.

Investigators said the drugs were often held in uncontrolled environments, such as car trunks, rented storage facilities and residences, thus compromising some of the drugs’ medical efficacy over time.

“It’s one thing when people sell their blood for money; it’s another when they sell their drugs, especially when the diversion compromises the pharmaceutical supply with tainted and outdated drugs,” NYPD Commissioner Raymond Kelly said in a statement.

Several of the defendants were also charged with narcotics trafficking offenses for buying and reselling drugs such as Oxycodone and Oxymorphone.

Follow Jennifer Booton on Twitter at @Jbooton