Maryland state and local governments will share an estimated $75 million as part of the settlement, Attorney General Douglas F. Gansler said. Maryland, Delaware and Kentucky also will split $150 million that will help consumers with mortgage forgiveness, forbearance and other types of aid.

"With this settlement, Maryland takes another step toward resolving the widespread economic damage caused by these lenders and the mortgage crisis they created out of sheer greed," Gansler said in a statement. "Marylanders will benefit as defrauded state and local government pension plans receive compensation and distressed consumers receive additional mortgage-related relief and assistance."

The deal is the largest legal settlement in history between a single U.S. company and the federal government. Last year, JPMorgan Chase agreed to a settlement of $13 billion for similar offenses. Citigroup paid $7 billion in a similar case this summer. Bank of America has paid more than $60 billion for recession-era misdeeds committed by companies now in its portfolio.

Speculation and negotiation has swirled for months as U.S. Attorney General Eric Holder's office has threatened to sue BofA for the sale of faulty mortgage securities to investors leading up to the financial crash in 2008. The allegations are tied to hundreds of billions of dollars in mortgage bonds sold to public and private investors.

Like previous claims, federal prosecutors contend Countrywide, Merrill and BofA sold those instruments to investors under the impression they were made up of high-quality mortgages, when, in fact, many of the loans were subprime notes doomed to default.

“We believe this settlement, which resolves significant remaining mortgage-related exposures, is in the best interests of our shareholders, and allows us to continue to focus on the future,” said BofA Chief Executive Brian Moynihan.

The settlement resolves actual and potential civil claims by the DOJ, the Securities and Exchange Commission and state attorneys general from Maryland, California, Delaware, Illinois, Kentucky and New York, the Federal Housing Administration and the Government National Mortgage Association (Ginnie Mae), as well as all pending claims against Bank of America entities brought by the Federal Deposit Insurance Corp.

Bank of America says the deal will result in a $5.3 billion negative impact on its third-quarter earnings, which is surely to wipe out all profits otherwise. In fact, the settlement value is close to the past three years worth of profits at the second-largest U.S. bank.

BofA shares were trading higher on the news Thursday morning, though investors had likely already factored the deal into estimates. The stock was up more than 1% to $15.70 in early trading.

While the settlement covers most outstanding civil claims tied to the sale of mortgage securities, it does not preclude the government from bringing criminal charges or charges against individuals. However, no major bank executives have faced such charges to date and statutes of limitations are either expiring or running out. Speculation emerged this week that former Countrywide CEO Angelo Mozilo may soon be sued for his role in the alleged sale of fraudulent mortgages.

Justice Department officials have been under intense pressure from the public and politicos to score massive settlements against the biggest U.S. banks. At a press conference this morning, Holder said he believed this deal with BofA sent a message.

“This historic resolution — the largest such settlement on record — goes far beyond ‘the cost of doing business,’” he said. "Under the terms of this settlement, the bank has agreed to pay $7 billion in relief to struggling homeowners, borrowers and communities affected by the bank’s conduct. This is appropriate given the size and scope of the wrongdoing at issue.”

BofA will pay $5 billion in a civil monetary fine under the agreement. It will pay an additional cash payment of $4.63 billion for remediation of wrongs, the announcement states.

Consumer relief will be distributed to struggling homeowners between now and 2018. It will come in the form of mortgage modifications, including first-lien principal and forbearance forgiveness and second-lien extinguishments, low- to moderate-income mortgage originations and community reinvestment and neighborhood stabilization efforts. The consumer relief will be subject to oversight by an independent monitor.