Bank of America said Wednesday it would set aside a massive $14 billion to settle claims from angry investors for losses on dodgy mortgage-backed securities in the country's housing collapse.

Hoping to put its disastrous 2007 purchase of mortgage lender Countrywide Financial behind it, Bank of America said it would pay a record $8.5 billion to a group of 22 large private investment groups who invested in packaged securities including poorly documented home loans from Countrywide.

The other $5.5 billion was for pending liabilities to other investors not included in the settlement.

It was the largest settlement by a financial services firm stemming from the financial crisis, which was kicked off by a crash in the market for investment products that packaged millions of poorly documented, often high-risk mortgages.

The payments would drive a loss for Bank of America's second quarter of up to $9.1 billion, after the first quarter's $1.7 billion gain.

It also piled on to the bank's net loss of $2.2 billion last year, after huge writeoffs mostly related to the housing industry.

"This is another important step we are taking in the interest of our shareholders to minimize the impact of future economic uncertainty and put legacy issues behind us," said Bank of America Chief Executive Officer Brian Moynihan.

"We will continue to act aggressively, and in the best interest of our shareholders, to clean up the mortgage issues largely stemming from our purchase of Countrywide."

Over the last six months, Bank of America has announced three agreements aimed at reducing the exposure to legacy mortgage issues related to Countrywide, once one of the country's largest home loan financiers.

In January, Bank of America announced agreements with two of its largest counterparties, Fannie Mae and Freddie Mac.

The 22 investment groups taking part in the settlement represented 530 separate residential mortgage-backed securitization trusts which had a principal valuation of some $424 billion.

They include major banks, insurance firms and investment houses including Goldman Sachs, BlackRock, PIMCO, Metropolitan Life, and ING Bank.

Also in the group was the US Federal Reserve's New York branch, through the "Maiden Lane" entities created to hold risky assets for which there were no buyers when Bear Stearns collapsed in 2008 and the government took over giant insurer AIG.

Ratings agency Moody's Investors Service said the settlement came in higher than it expected but that it cleared out a looming liability that had weighed on the bank's credit rating.

"While the settlement and additional provisions will negatively affect BAC's earnings and capital ratios in the current quarter, the bank's capital ratios will remain above the level of a year ago," Moody's said in a statement.

"Following today's settlement and the announced addition to reserves, Moody's believes that (the bank's) remaining representation and warranty exposures are no longer a negative credit concern," said David Fanger, Moody's Senior Vice President, referring to the $5.5 billion set aside.

Bank of America shares were up 3.1 percent to $11.16 in late morning trade after the news.