The biggest corporate takeover battle in U.S. history ended last night with an agreement to sell RJR Nabisco Inc. to the Wall Street investment firm of Kohlberg, Kravis Roberts and Co. for more than $24.5 billion.

The winners, known on Wall Street as KKR, outbid RJR Nabisco's top executives and the investment firm of First Boston Co. with an offer that will require them to borrow more money than any company has ever borrowed before.

Their offer far exceeds the highest price previously paid for a U.S. corporation, the $13 billion purchase of Gulf Oil Corp. by Chevron Corp in 1984.

RJR Nabisco is the 19th-largest industrial company in America, a $15.8 billion-a-year business with 120,000 employees and a storeful of the nation's best-known brands -- Camel, Winston and Salem cigarettes, Lifesavers, Baby Ruth and Butterfinger candy bars, Planters Peanuts, Animal Crackers, Ritz, Triscuits, Sugar Honey Grahams, Shredded Wheat, Oreo cookies and Del Monte foods.

The decision to sell the company to KKR was a stunning setback for RJR Nabisco's Chairman F. Ross Johnson and a group of top executives. Six weeks ago they set out to buy the business for themselves because they believed its stock price was depressed by public concerns about smoking and the future of the tobacco business.

But other investors as well decided the company was worth more than the $56 a share its stock was selling for, triggering a bidding war after Johnson and his group of insiders made their initial offer of $75 a share.

Responding to widespread criticism that insiders have an unfair advantage in corporate buyouts, RJR's board asked for sealed bids that were delivered at 5 p.m. Tuesday. But after the bids were opened and his group came up second best, Johnson threw the decision-making into turmoil by submitting a new offer yesterday morning.

Johnson's move put RJR's directors in a no-win situation. If they rejected Johnson's offer, they risked being sued by shareholders for not taking the highest bid. If they accepted the management deal, they could be sued by the unsuccessful bidders for not playing by their own rules.

The decision-making process was mined with potential conflicts of interest because more than half the 17 members of the RJR Nabisco board of directors have links of one kind or another to Johnson's group, including three other board members who are partners in his bid.

A committee of the RJR Board began virtual nonstop negotiations after the bids came in Tuesday, giving KKR a chance to raise its offer in response to Johnson's new bid. First Boston's bid apparently was rejected early on because it contained so many contingencies that the board feared the deal could not be closed.

Shortly before midnight last night, RJR issued a statement saying it had signed an agreement with KKR.

KKR offered shareholders -- who still must ratify the deal -- $81 in cash, $18 of preferred stock and other securities worth $10 a share. Johnson's group had evaluated its offer at $112 a share, but the board said in a statement it considered the two bids "substantially equivalent" and "adopted the KKR offer as being in the best interest of the company and its shareholders."

Henry Kravis and George Roberts, the principals of KKR, issued a statement saying, "RJR, its employees, consumers, suppliers and communities can be assured that the business will now return to normal and stability will be restored."

KKR will acquire RJR through what is known as a leveraged buyout. It will borrow virtually all the money, using RJR itself as collateral. The loans will be paid back from the company's future profits and by selling off some of its divisions. KKR is expected to sell several food divisions and keep RJR's tobacco business, its most profitable operation.

Part of the money will be borrowed from banks and the remainder raised by issuing high yield bonds, known junk bonds.

KKR is a small firm whose specialization in leveraged buyouts has enabled it to buy several giant businesses that add up to a conglomerate second in size only to General Electric Co. It bought Safeway when that company was threatened by a takeover by the Haft family of Washington and in similar situations acquired Beatrice Cos. Inc. and Owens Illinois. To a large extent, RJR Nabisco went on the block because of health concerns about smoking. The tobacco business, though enormously profitable, is out of favor with investors who fear its future is clouded by lung cancer, heart disease and other tobacco-related ailments that are steadily reducing the number of Americans who smoke. As a result, tobacco company stocks have been selling at relatively low prices.

Frustrated by the failure of RJR Nabisco stock to climb despite a 13 percent increase in profits last year -- to $1.2 billion -- Johnson decided to make an offer to buy the company.

While the talks were under way, RJR stock surged in frantic New York Stock Exchange trading yesterday, and the Big Board briefly halted it when buy orders overwhelmed sell orders. The stock resumed trading by midafternoon and closed up $2.12 1/2 to $93 on volume exceeding 6.7 million shares.

The bids for the company were so complicated that RJR Nabisco's board is paying two major investment firms $14 million apiece just to evaluate them.

Those fees for Lazard Freres & Co. and Dillon Read are part of an estimated $300 million to $400 million that Wall Street firms will make on the RJR Nabisco deal regardless of who wins.

The banks providing loans are expected to collect up-front loan commitment fees in the $170 million range based on their usual rate. Arranging other financing will cost the eventual winner an additional $150 million.

Investment bankers, lawyers, public relations people, printers and others will reap millions more, and the Securities and Exchange Commission will get more than $1 million in filing fees just for processing the paperwork.