WASHINGTON — Freight rail companies and unions representing tens of thousands of workers reached a tentative agreement to avoid what would have been an economically damaging strike, after all-night talks brokered by Labor Secretary Martin J. Walsh, President Biden said early Thursday morning.

The agreement now heads to union members for a ratification vote, which is a standard procedure in labor talks. While the vote is tallied, workers have agreed not to strike.

The talks brokered by Mr. Walsh began Wednesday morning and lasted 20 hours. Mr. Biden called in around 9 p.m. Wednesday, a person familiar with the talks said, and he hailed the deal on Thursday in a long statement.

“The tentative agreement reached tonight is an important win for our economy and the American people,” Mr. Biden said. “It is a win for tens of thousands of rail workers who worked tirelessly through the pandemic to ensure that America’s families and communities got deliveries of what have kept us going during these difficult years.”

The White House did not immediately release details of the agreement. Talks had stalled over a push for companies to improve working conditions, including allowing workers to take unpaid leave to visit physicians.

“These rail workers will get better pay, improved working conditions, and peace of mind around their health care costs: all hard-earned,” Mr. Biden said. “The agreement is also a victory for railway companies who will be able to retain and recruit more workers for an industry that will continue to be part of the backbone of the American economy for decades to come.”

The Association of American Railroads, an industry group, thanked the unions and Biden administration officials — including Mr. Walsh, Transportation Secretary Pete Buttigieg and Agriculture Secretary Tom Vilsack — for helping to bring the deal together.

“Thanks to the dedication of all members involved in the collective bargaining process,” the association said in a news release, “these new contracts provide rail employees a 24 percent wage increase during the five-year period from 2020 through 2024, including an immediate payout on average of $11,000 upon ratification.”

Mr. Walsh wrote on Twitter that the agreement “balances the needs of workers, businesses, and our nation’s economy.”

“Our rail system is integral to our supply chain,” he said in a follow-up tweet, “and a disruption would have had catastrophic impacts on industries, travelers and families across the country.”

Mr. Biden and his economic team had increasingly inserted themselves in the talks over the past week, hoping to avoid a work stoppage that would have snarled the distribution of food, chemicals for water treatment plants and other critical goods across the country. Such a stoppage also risked creating shortages on store shelves that could have sent consumer prices soaring, further adding to an inflation rate that reached a four-decade high this summer.

Unions and the freight rail industry were negotiating ahead of a Friday deadline, when a federally imposed “cooling-off period” was set to end and workers would have been free to strike if no deal had been reached. That possibility had already shaken both freight and passenger rail companies.

Nearly a third of U.S. freight moves by rail, second only to trucking. The Association of American Railroads estimated that a nationwide rail service interruption would have idled more than 7,000 trains daily and cost the economy more than $2 billion a day.

Railroads began warning their customers last week that they would prepare for a strike by cutting back some services. Union Pacific, CSX and BNSF all said that they would begin securing hazardous and toxic materials on Monday to try to ensure that dangerous goods would not be left unguarded in the event of a strike. Norfolk Southern closed its gates to shipping containers coming off trucks and ships on Tuesday, and said it planned to begin shutting down its network entirely at midnight on Thursday.

On Wednesday, in anticipation of a strike, Amtrak said it would cancel all long-distance passenger trains beginning on Thursday in order to avoid stranding people given that many of its trains run on tracks operated and maintained by freight carriers.

Administration officials had begun making contingency plans for trying to minimize disruptions for critical shipments in the event of a strike. Those plans included working with trucking companies, ocean shippers and other alternative forms of transportation to ensure some supplies could still get to their destinations.

Ana Swanson contributed reporting.