Johnson & Johnson agreed to pay more than $20 million to avoid the first federal trial in which it faced accusations of helping fuel the opioid epidemic by mishandling its painkillers.

Under the deal, announced late Tuesday, J&J will pay two Ohio counties $10 million in cash, $5 million to cover their legal expenses and make $5.4 million in charitable donations to groups fighting the opioid epidemic. The company didn’t admit any wrongdoing as part of the deal.

It’s the first time J&J, the world’s largest maker of health-care products, agreed to settle an opioid-related lawsuit. The company went to trial to fight Oklahoma’s allegations that it illegally marketed the medicines and lost. A judge ordered J&J to pay $572 million in damages.

“I guess they didn’t want to face the prospect of a jury that could find they should pay billions over their handling of the opioid painkillers,” said Richard Ausness, a University of Kentucky law professor who teaches about mass torts. “They certainly got out cheap. $20 million is chump change to them.”

J&J is the fourth opioid maker to wave the white flag in return for being excused from a trial scheduled for Oct. 21 in Cleveland. Last month, generic-opioid manufacturer Mallinckrodt Plc settled for $30 million. A unit of Endo International Plc offered to pay $10 million and donate $1 million worth of drugs to avoid the trial and Allergan Plc agreed to pay $5 million.

“The settlement allows the company to avoid the resource demands and uncertainty of a trial as it continues to seek meaningful progress in addressing the nation’s opioid crisis,” J&J officials said in a statement. The company, along with other opioid makers and distributors, are in talks with states, cities and counties that have sued in hopes of reaching an overarching agreement to resolve more than 2,000 lawsuits.

“J&J has joined the ever-increasing ranks of companies willing to right terrible wrongs and pay to the counties funds they so desperately need and amply deserve,” Paul Hanly, an attorney for some of the municipalities, said in an emailed statement.

The settlement leaves Teva Pharmaceutical Industries Ltd. as the last opioid maker slated to go to trial. Jurors also will consider claims against drug distributors McKesson Corp., Cardinal Health Inc. and AmerisourceBergen Corp. Those companies are accused of turning a blind eye to red flags about unusually large opioid shipments and employing lax compliance standards to rake in billions in profits.

Some hard-hit towns in Ohio, West Virginia and Kentucky have been flooded with millions of the addictive pills over the years, leading to an epidemic that claims the lives of more than 100 Americans a day.

The Cleveland trial originally included more than a dozen pharma companies involved in the opioid segment of the business. The Ohio counties’ lawyers have used settlements and other procedural maneuevers to winnow down the number to a more manageable level, said Carl Tobias, a University of Richmond law professor who teaches about product-liability issues.

“It makes the case easier to present to the jury and easier for the judge to manage,” Tobias said. “They only have a limited amount of trial time in which to make their case, so the fewer defendants, the better.”

The case is In Re National Prescription Opioid Litigation, 17-md-2804, U.S. District Court, Northern District of Ohio (Cleveland).