After a boardroom battle that lasted less than two months, a rookie activist investor has upended management of CSX Corp., installing a 72-year-old industry maverick as chief executive with a mandate to slash costs and revamp one of the country’s biggest railways.

CSX said Monday it agreed to appoint Hunter Harrison, effective immediately. The decision ends an unusual contest with an activist investor who quickly parlayed Mr. Harrison’s popularity with Wall Street into sweeping leadership changes at CSX, which operates one of two major freight networks east of the Mississippi River.

Mr. Harrison’s success turning around other railways has earned him such a loyal following that news of his ambition to run CSX triggered a $10 billion surge in its share price in mid-January, even before his activist partner Mantle Ridge LP had a chance to detail plans to shake up management to the board or confirm the investment publicly.

From the start, Mr. Harrison’s partnership with Mantle Ridge’s founder Paul Hilal drew the attention of the activist-investing world eager to see how far Mr. Harrison’s star could carry a newcomer with a stake of only 4.9% in CSX.

Mr. Hilal, a former partner with Pershing Square Capital Management LP, launched Mantle Ridge last year. This was his first solo investment since leaving Pershing Square, where he was the right-hand man to William Ackman.