The deal, one of several corporate settlements being unveiled during the last days of President Barack Obama's administration, came as U.S. authorities grapple with an opioid drug epidemic.

"This settlement demonstrates the accountability and responsibility that go along with handling controlled prescription drugs," U.S. Drug Enforcement Assistant Administrator Louis Milione said in a statement.

A Costco spokesman declined to comment.

Under the settlement, Costco acknowledged that from 2012 to 2015 some of its pharmacies dispensed drugs without complying with the Controlled Substances Act and related regulations, the Justice Department said.

Those violations included filling prescriptions that lacked valid DEA numbers; filling prescriptions outside the scope of a doctor's DEA registration; and failing to maintain accurate dispensing records, the Justice Department said.

"These are not just administrative or paperwork violations - Costco's failure to have proper controls in place in its pharmacies played a role in prescription drugs reaching the black market," said U.S. Attorney Eileen Decker in Los Angeles.

The Justice Department said Costco had bought a new pharmacy management system and set up a three-tier audit program at its pharmacies to address the issues.

Under the settlement, DEA officials will be allowed to conduct unannounced and unrestricted inspections of all DEA-registered Costco pharmacies for the next three years, the Justice Department said.

(Reporting by Nate Raymond in New York; Editing by Bernadette Baum and Daniel Wallis)

By Nate Raymond