Justin Sullivan/Getty Images

Bank of America and MBIA, the troubled bond insurer, have reached a $1.7 billion agreement to settle a long-running legal dispute over mortgage-backed securities that became troubled when the financial crisis blossomed.

The deal, which was announced on Monday, provides a lifeline to MBIA, which stood in danger of being unable to meet its obligations in a few weeks’ time. And the agreement will turn Bank of America from bitter foe to equity investor in, and major lender to, the insurer.

The battle between the two financial giants had its beginnings in transactions before the financial crisis between MBIA and two companies — Merrill Lynch and Countrywide Financial — that Bank of America acquired during the crisis.

Merrill Lynch was in a position to collect billions from the insurer — if it had enough cash to meet its obligations.

But it seemed unlikely that MBIA could come up with the cash unless Countrywide agreed to pay billions to settle claims that it had misled the insurer regarding the quality of mortgages in securitizations insured by MBIA, and had failed to honor its obligations to repurchase those mortgages.

Now the multibillion-dollar claims the institutions had against each other will be canceled out. Under the agreement, Bank of America will pay $1.6 billion in cash to MBIA, and will lend the firm another $500 million. It also acquired warrants that, if exercised, would give the bank a 4.9 percent stake in the insurer. In addition, the bank will surrender to MBIA about $130 million in MBIA bonds.

Bank of America said the agreement would reduce its previously announced first quarter after-tax profits by $1.1 billion, or 10 cents a share, but would improve its capital position. MBIA indicated that the settlement would have little impact on its profits, but said its first-quarter results, scheduled to be released on Thursday, would be delayed.

The agreement also appears to remove any practical chance that MBIA’s split into two companies will be reversed. One company, National, is taking over the municipal bond insurance policies. The other, known as MBIA Corporation, backs the insurance the company issued on securitized products and on credit-default swaps. Both remain subsidiaries of the parent, MBIA Inc.

Jay Brown, MBIA’s chief executive, said the agreement “sets the stage for National to reclaim its leadership in the U.S. public finance insurance market.”

Shares of MBIA soared on word of the settlement, rising $4.46, or 45 percent, to $14.29. Bank of America shares surged 64 cents, or 5 percent, to $12.88.

Before the settlement, it appeared that within weeks the insurer would owe as much as $3 billion to Merrill Lynch stemming from credit-default swaps issued by MBIA concerning commercial real estate transactions.

MBIA Corporation had met earlier obligations by borrowing money from National, the municipal bond part of the company.

Nathaniel Brooks for The New York Times

But Benjamin Lawsky, the New York State financial services superintendent, refused to allow further loans, and it was expected that MBIA Corporation might be put into receivership before it had to pay Merrill Lynch.

In an interview, Mr. Lawsky said negotiations had been going on for more than a year, but accelerated in the last couple of weeks. “There was a way to get to yes because everyone had claims on everyone else,” he said. The agreement, he said “resolves significant exposure and expensive litigation for Bank of America, while also giving MBIA a path forward.”

MBIA originally insured municipal bonds, a profitable business because few such bonds ever defaulted and municipalities buying the insurance would get AAA ratings on their bonds, saving them more in interest payments than the insurance cost because many muni bond investors were very risk-averse.

It later expanded into insuring securitizations and writing credit-default swaps, businesses in which the purchasers were more sophisticated. When the financial crisis struck, it became clear that the company had taken some very bad risks.

In 2009, the New York State Insurance Department, which later was folded into the Department of Financial Services, approved a decision by MBIA to split into the two companies. Banks and hedge funds that owned securities insured by MBIA went to court to try to reverse the split. In March, MBIA prevailed in one of those suits, as Justice Barbara R. Kapnick of the New York State Supreme Court ruled that the insurance department had wide latitude to approve the split with or without much investigation.

She cited a deposition by Michael Moriarty, the then-deputy superintendent of the department, who said “the department did not, nor do they usually, verify the financial condition of a company.” Since that was the policy, the judge concluded she had no authority to question it, even if some information provided by the company was false.

Bank of America had vowed to appeal that decision, raising the possibility that an appellate court would order a trial that could be embarrassing to the state regulator. Under the settlement, the bank will drop that appeal.

Some MBIA legal disputes remain. Société Générale, the French bank, remains a plaintiff in the suit over the split, and that dispute seems likely to cost MBIA perhaps $200 million to settle. MBIA is also pursuing complaints against ResCap, a mortgage company previously owned by General Motors’ finance arm, and against Credit Suisse, the Swiss bank. Settlement of those disputes potentially could bring in more than $500 million to MBIA.

Another suit against the MBIA split was filed by hedge funds that feared their insurance claims would not be honored. That suit is still pending, but the agreement would make it harder to pursue since all the information the bank obtained in disclosure from MBIA would be returned to the insurer.

Bank of America had bought the bonds in an effort to frustrate MBIA’s effort to change their terms. By turning over the bonds to the insurer, it now seems likely MBIA can change the terms. The expectation is that MBIA will eventually resell the bonds to investors, providing cash for the company.

The money provided by Bank of America will largely be used to repay the loans National made to MBIA Corporation, solidifying National’s finances.

Under the warrants, Bank of America will have the right for the next five years to purchase 9.94 million shares of MBIA for $9.59 a share. If the warrant is exercised, that would provide the insurer with another $95 million.