The two sides reached the agreement following 20 hours of intense talks brokered by the Biden administration. The deal now goes to the unions to be voted on, a person familiar with the talks told Reuters.

Shares of Union Pacific rose 4% and CSX gained 2.5%.

If a deal had not materialized before the deadline of one minute after midnight on Friday, the resulting legal worker strikes could have cost the U.S. economy $2 billion per day and led to food and fuel supply disruptions across the country.

"(Railroads) have averted this industrial unrest, which can only be good for their financial performance," said Susannah Streeter, senior investment and markets analyst at Hargreaves Lansdown.

Negotiations between railroads including Union Pacific, Berkshire Hathaway's BNSF, CSX, Norfolk Southern and Kansas City Southern and a dozen unions had stretched on for more than two years.

The deal also offers some relief to investors already grappling with the impact of the Federal Reserve's relentless interest rate hikes to curb surging inflation.

Wall Street suffered its worst day in more than two years on Tuesday after a hot August inflation report fueled bets of more aggressive monetary policy tightening.

"Digging deeper, many might have wondered how much of an impact a strike might have on rate rise discussions due to the potential impact on inflation," said Danni Hewson, financial analyst at AJ Bell.

(Reporting by Medha Singh and Bansari Mayur Kamdar in Bengaluru; Editing by Devika Syamnath)

By Medha Singh and Bansari Mayur Kamdar