Since John Mackey co-founded a precursor to Whole Foods in 1978, he has taken a hands-on management approach to the organic grocery chain. He personally oversaw each step of its national expansion. During a takeover battle 10 years ago, Mr. Mackey used a pseudonym to write anonymous blog posts attacking a competitor.

So over the last two years, as Whole Foods’ stock price sunk and activist investors called for changes, Mr. Mackey took the challenges to his company personally. After three decades of running a beloved chain of upscale grocers that had helped revolutionize the way Americans ate, Mr. Mackey, a hippie who rails against greed, was mired in a bare-knuckle fight with Wall Street.

Last year, Mr. Mackey tried to reassert control, pledging to become more hands-on and address investor concerns.

But behind the scenes, the forces that would lead Whole Foods to agree to sell itself to Amazon for $13.4 billion on Friday were already at work.

In September, Neuberger Berman, a $267 billion investment firm that had been quietly building its own position in Whole Foods stock, sent a letter to the board of directors that highlighted issues with the company’s business. In November, Walter Robb, Mr. Mackey’s co-chief executive and heir apparent, stepped down. Mr. Mackey was once again leading the company on his own and personally pledged to increase margins and revamp operations.

Those changes were not enough for Neuberger Berman, and its executives began a behind-the-scenes campaign to shake up Whole Foods last year, seeking to enlist the help of activist hedge funds. Among the things they had discussed were strategic options for the company to pursue.

Then, in April, Jana Partners, an $8.5 billion activist investor, announced its position as the company’s largest outside shareholder. Jana outlined a laundry list of concerns it said the company needed to address, including the way it had conducted its business, and took aim at what it said were deficiencies in customer service and distribution strategy, as well as analytics and brand development. The hedge fund also put forth a list of four nominees to replace members of the Whole Foods board.

Mr. Mackey was outraged at the developments, which took place on the eve of a scheduled book tour, and vowed a fight.

“These people, they just want to sell Whole Foods Market and make hundreds of millions of dollars, and they have to know that I’m going to resist that,” Mr. Mackey told Texas Monthly. “That’s my baby. I’m going to protect my kid, and they’ve got to knock Daddy out if they want to take it over.”

But Jana had hit on issues that other investors, including Neuberger Berman, had raised. In April, Neuberger Berman wrote another letter urging the company to explore the possibility of a sale.

In an effort to broker peace with Jana, Whole Foods said it was willing to consider some of the candidates that Jana put forward for the board. After interviewing four of them, Whole Foods said it would consider two, on the condition that Jana back down from its public aggression, Mr. Mackey said. But Jana declined to compromise.

Image

An upscale grocery chain, Whole Foods helped revolutionize the way Americans eat, with its focus on healthy foods. More recently, it became a target of criticism from activist investors.CreditDolly Faibyshev for The New York Times

As Whole Foods’ troubles spilled into public view, talk about a sale of the company began to swirl. Amazon — the leading force in e-commerce — was among the companies that had initial discussions with the Whole Foods board. Whole Foods was unsure of how serious Amazon was — Amazon’s founder and chief executive, Jeff Bezos, had never struck a deal of this size before — and the talks did not move fast at first.

Soon after that, Whole Foods announced its own board overhaul and pledged to improve short-term results. Gabrielle Sulzberger, a private equity executive who is married to Arthur O. Sulzberger Jr., the chairman and publisher of The New York Times, was named the company’s chairwoman.

After the Whole Foods board shake-up, talks with Amazon accelerated. Facing unrelenting pressure from Wall Street, the supermarket’s board soon decided that pursuing a sale to the online colossus was the best move, with Amazon providing technological know-how and the promise of bolstering operational efficiency.

On Friday, Whole Foods announced it would sell itself to Amazon, a transaction — code-named “Project Tree” by bankers — that stands to reshape the entire grocery industry, swaths of e-commerce and possibly the retail landscape at large. By late Friday, Whole Foods stock rose above Amazon’s offer of $42 a share, as investors bet on a potential bidding war over the upscale market chain.

For a transaction that promises seismic changes, the talks between the two companies were fairly swift, according to people with knowledge of the matter.

For Whole Foods, whose ethos was once so crunchy granola that it did not originally sell beer or meat, Amazon and its vast empire of warehouses and robots might seem an odd match. But the deep pockets of Mr. Bezos offer Whole Foods a way to escape the harsh glare of public investors who demanded cost cuts, shake-ups in management and on the board and more.

Mr. Mackey did not speak to the press on Friday. A spokesman for Jana also declined to comment.

Inside Whole Foods, the news of the sale to Amazon was greeted with a sense of relief, according to employees who agreed to talk on the condition of anonymity because they did not have permission to speak to the news media.

Charles Kantor, who oversees Neuberger Berman’s main investment in Whole Foods, said his team was caught by surprise when the news broke.

“It obviously happened very quickly,” Mr. Kantor said. “Remember, the board just changed, and it was all Mackey’s lieutenants who would have liked nothing more than to come out on top.”

Mr. Kantor also indicated that he expected the Whole Foods board to consider any other bids that were made.

“I don’t think this is the final chapter,” he added “Amazon gets one of the best brands in the world for a very thrifty multiple.”

Although Mr. Mackey was able to sell Whole Foods on his own terms, the activist investors he so loathes came out on top, too.

From its brief investment in Whole Foods, Jana Partners, according to data compiled by Bloomberg, stands to make about $301 million.

Stephanie Strom contributed reporting.

A version of this article appears in print on , on Page B1 of the New York edition with the headline: Whole Foods Finds an Unlikely Refuge. Order Reprints | Today’s Paper | Subscribe