Martin Marietta Corp. yesterday appeared to have escaped from the most bizarre corporate takeover battle ever, surviving as an independent company while its pursuer, Bendix Corp., was to be taken over by Allied Corp. in a settlement being worked out by the three companies, according to sources.

Officials of the three companies, their investment bankers and lawyers worked into the night to iron out details of the deal, which apparently would swap Marietta's holding in Bendix to Allied for Bendix's Marietta stock, sources said. Marietta early yesterday bought 10 million shares of Bendix, forcing the negotiations. Related stories on Page D8.

Spokesmen for the three companies said official word on the agreement probably would not come until today. Sources said the final settlement was being delayed by negotiations over terms of the stock swap, which also could include the exchange of some assets of one or more of the companies.

The negotiations began after Allied backed away from a plan announced Wednesday to take over both Bendix and Marietta. Bendix had struck the deal with Allied in a last-ditch effort to fend off Martin Marietta's attempts to buy Bendix. Under the plan, Allied would have offered to purchase Bendix, which owns 70 percent of Marietta, and then finish Bendix's acquisition of Marietta, at a total cost of $2.3 billion.

But that deal soured when Martin Marietta bought a 44 percent block of Bendix early yesterday for $750 million and announced plans to keep buying Bendix stock until it got a majority of the company.

Even though Martin Marietta's offer for Bendix of $75 a share was below Allied's offer of $85, sources said many investors seemed to fear that Allied's offer might be illusory -- a tactic to force Martin Marietta out of the fight. So they sold their stock to Marietta thinking that offer was more of a sure thing.

Analysts said the Martin Marietta victory was made possible by a crucial tactical error by Bendix, which waited too long to enlist Allied's help. Had Bendix and Allied joined forces a day earlier, experts said, federal law would have blocked Martin Marietta from buying Bendix stock yesterday long enough for Bendix to have obtained formal control of the company and terminated the offer.

But Martin Marietta's victory was not without drawbacks. The month-long battle has seriously strained the company's financial position, analysts said, and officials of the aerospace conglomerate will likely spend the immediate future trying to clean up the economic debris. Perhaps their most important task will be to reissue the Marietta stock re-obtained from Bendix to raise funds to reduce the massive debts run up in the takeover battle.

The biggest loser, however, appeared to be Bendix Chairman William Agee, who touched off the fight a month ago with an offer for Martin Marietta only to have it blow up in his face. Agee will apparently become president of Allied and remain as chairman of Bendix, which will be a wholly-owned subsidiary of the petrochemical giant, according to sources close to the negotiations. Agee had already received one embarrassing blow Wednesday night, when four directors resigned in protest of the Allied deal.

The surprise finish came after days of frantic negotiations and maneuvering by officials of Martin Marietta, Bendix and Allied. A fourth company that had been involved in the battle as Marietta's ally, United Technologies Corp., put its bid for Bendix on ice last week.

Bendix bought 70 percent of Martin Marietta a week ago, giving itself a temporary advantage in the fray, but was prevented by Maryland state law from taking immediate control of Marietta. When Bendix was unable to convince a court to block the offer by Martin Marietta, the Bethesda company went ahead early yesterday and bought Bendix stock.

Marietta bought 44 percent of Bendix right away, and a spokesman said yesterday afternoon additional stock "was rolling in."

Under laws of Delaware, where Michigan-based Bendix is incorporated, Marietta could take immediate control of Bendix once it obtained more than half of its stock. Marietta's strategy was to obtain a majority of Bendix, take control of the company, and then merge Bendix and Marietta. Allied said it would drop out of the bidding if it could not get 51 percent of Bendix.

Bendix and Martin Marietta attempted Tuesday to work out a compromise, but talks broke off because Martin Marietta directors felt an obligation to Bendix shareholders to go through with the offer for their stock.

In addition to the Bendix and Martin Marietta executives attending the Tuesday meeting, Agee was joined by his wife, Mary Cunningham, an executive at Joseph E. Seagram & Sons and a former Bendix official who sources said participated from the start in the planning to buy Martin Marietta. One source described Cunningham's unusual presence at the meetings with Marietta as an "irritation" to the Marietta officials.

Bendix officials had been fairly confident of victory earlier in the week. They felt that with a 70 percent holding in Marietta, they could convince a court to block Martin Marietta's counteroffer as a waste of assets technically owned by Bendix. In a hearing in federal district court in Baltimore Tuesday, Bendix attorneys accused Martin Marietta officials of following a "scorched earth" strategy of disabling the company before Bendix could take control of it.

But Bendix's legal challenges to the Marietta offer were unavailing, and Martin Marietta was able to buy the stock after midnight Wednesday, a couple of hours after Bendix's last appeal was rejected.

The combination of Bendix and Allied creates a petrochemical and industrial giant with combined 1981 sales of $10.8 billion and profits of more than $800 million. The acquisition of Bendix fits in well with Allied Chairman Edward L. Hennessy Jr.'s oft-stated desire to move the former Allied Chemical away from chemicals and into other fields. Bendix brings to the picture interests in auto parts, aerospace electronics, and machine tools.

Bendix had desired Martin Marietta because of Agee's desire to expand his company away from recession-vulnerable industries into the defense contracting field. One of the nation's largest defense contractors, Martin Marietta is a major contractor on the B1 bomber, MX missile and many other missile systems, as well as the space shuttle. Martin Marietta also has interests in chemicals, aluminum, cement and sand and gravel. The company earned $200 million last year on sales of $3.3 billion.