William A. Ackman, a billionaire investor, staked his reputation as a savvy stock picker on his ability to oversee a turnaround at Valeant Pharmaceuticals International.

When other hedge funds began to leave Valeant in 2015 as questions about its practices for pricing drugs and its accounting procedures mounted, Mr. Ackman bravely — some might say stubbornly — stood his ground.

To defend his big bet on Valeant, he held a marathon conference call with investors and reporters in October 2015. He then effectively doubled down by adding shares and taking two board seats with the company — including one for himself. Last year, he sought to mollify angry senators at a hearing on Valeant’s pricing.

But none of that was enough to stop the bleeding for a stock that had plunged from a high of $257 in the summer of 2015. While the stock market has surged since the presidential election in November, Valeant shares have continued to sag, closing at $12.11 on Monday.

So Mr. Ackman threw in the towel and announced that his firm, Pershing Square Capital Management, was dumping one of its worst investments ever: a two-year bet that lost about $4 billion for his investors.

After the close of trading on Monday, Pershing Square said it had sold some 27 million shares and options in Valeant, its entire stake.

The decision to part ways with Valeant is another black eye for the silver-haired Mr. Ackman, who was once revered by many on Wall Street — a “Baby Buffett,” according to Forbes magazine — for his penchant for picking the right companies for a management or structural shake-up.

Valeant was a steep challenge. Once a darling of the stock market, Valeant had appeared to be changing the business model for the pharmaceutical industry with its relentless acquisitions. But questions over its aggressive accounting and the political fire over its drug pricing — on top of a huge pile of debt — proved to be too much.

In addition to Pershing Square’s troubles with Valeant, it has been punished by the performance of Herbalife, the nutritional food supplement company that Mr. Ackman has said is an illegal pyramid scheme and will ultimately collapse.

Several years ago, Mr. Ackman began a $1 billion wager that Herbalife shares would collapse — something that has not happened even though the Federal Trade Commission last year fined the company and ordered it to restructure its sales practices.

When troubles mounted at Valeant, which was forced to change chief executives, Mr. Ackman became increasingly involved in trying to turn around the company’s fortunes. But Valeant was ultimately responsible for big losses incurred by the hedge fund the last two years — humbling for Mr. Ackman and rough for his investors.

As of March 7, Pershing Square Holdings, a publicly traded portfolio that mirrors the main hedge fund, was down 1.5 percent for the year. Last year, the portfolio was down 13.5 percent, and in 2015, the portfolio was down 20.5 percent.

Over all, Mr. Ackman’s hedge fund firm has had more success than failure, registering nine winning years since it began in 2004.

The move by Mr. Ackman to part ways with Valeant has similarities with Pershing Square’s big investment in J. C. Penney, where he also had a board seat. Mr. Ackman made a huge bet on a turnaround strategy at J. C. Penney only to see it fail miserably, selling his stake in the company in 2013.

In the announcement about Valeant, Mr. Ackman said he and Stephen Fraidin, a Pershing Square vice chairman, would remain on Valeant’s board until the company’s annual meeting but would not stand for re-election.

Pershing Square, which has more than $11 billion in assets, said in its statement that the sale would enable it to “realize a large tax loss, which will enable us to dedicate more time to our other portfolio companies and new investment opportunities.”

Mr. Ackman began buying into Valeant in 2015 and has said the average share price his firm paid was $190. As recently as the fall, he was defending the investment and the decision to gain two board seats.

“I have an enormous stomach for volatility,” he told an audience in November at the DealBook conference sponsored by The New York Times.

In Pershing Square’s statement, Mr. Ackman did not offer any reason for getting out of Valeant now or explain the decision to let his firm sell its shares. But the statement said, “We wish the company and its extremely hard-working, dedicated and loyal employees great success in the future.”