WASHINGTON - Bank of America is paying nearly $17 billion in penalty to settle a federal probe over its sale of faulty mortgage-backed securities that largely caused the financial crisis in 2008, the United States Justice Department said Thursday.

The settlement deal by the nation's second largest bank tops a $13 billion agreement between the Justice Department and J.P. Morgan Chase last year over similar issues. The department last month announced a $7 billion settlement with Citigroup in another case of similar nature.

"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."

The beneficiaries were hit hard by the housing market crash that triggered the Great Recession. The settlement also includes $9.65 billion in fines from the bank which is the 21st largest company by total revenue and second largest bank holding company by assets in the US.

"And just like you might be in for an unpleasant surprise when you got home and poured yourself that glass of milk, investors -- such as public pension funds and federally insured financial institutions -- were unpleasantly met with billions of dollars in losses when those securities investments soured," he said.

Most of the faulty loans that backed the securities came from firms the bank acquired in 2008. These included Countrywide Financial Corp. of Calabasas and Wall Street investment bank Merrill Lynch Co.

The justice department alleges the banks knowingly sold investors toxic mortgages that included so-called subprime home loans taken out by homeowners who were unable to maintain the payments.

The poor quality of the loans led to huge losses for investors who bought mortgage-backed securities. This ultimately led to the worst economic downturn since the Great Depression of the 1930s.

The deal comes after weeks of a legal battle and intense negotiations during which the bank refused to offer more than $13 billion, including cash and consumer relief.