Mr. Azar “may either collect the data necessary to set payment rates based on acquisition costs, or he may raise his disagreement with Congress,” but he may not circumvent the mandate of Congress, said Judge Contreras, who was appointed by President Barack Obama. The government had acknowledged that it did not know the precise amount of the difference between what hospitals were paying for the drugs and what Medicare was reimbursing them.

The program, created under Section 340B of the Public Health Service Act, is commonly known as the 340B program.

Caitlin Oakley, a spokeswoman for Mr. Azar, said Monday: “We are disappointed with the court’s ruling and are evaluating next steps. As the court correctly recognized, its judgment has the potential to wreak havoc on the system.”

Ms. Oakley said the decision could increase costs for Medicare patients, who are generally responsible for 20 percent of the Medicare-approved amount for outpatient drugs covered by the program. Most people on Medicare have supplementary insurance, like a Medigap policy or retiree health benefits, to help pay their share of the bill.

The lawsuit challenging the Medicare cuts was filed by the American Hospital Association; by two trade groups representing teaching and public hospitals; and by three providers: Henry Ford Health System, based in Detroit; Park Ridge Hospital, in Hendersonville, N.C.; and Eastern Maine Healthcare Systems, now known as Northern Light Health.

Dr. Robert A. Chapman, a medical oncologist at Henry Ford Health System, described the administration’s action as an example of “reverse Robin Hood.” Under the policy, he said, the government took money from hospitals serving large numbers of low-income people and redistributed most of it to hospitals that did not qualify for the program.

When Medicare cuts its payments to hospitals, Dr. Chapman said, it tends to offset the discounts that hospitals receive from drug manufacturers.