JPMorgan has agreed to pay $5.1 billion to Fannie Mae and Freddie Mac to resolve claims stemming from the housing bubble, federal housing regulators announced Friday.

The bank has also been in talks with the Justice Department and other government officials over another potential settlement based on similar claims. That settlement will likely be even more expensive for JPMorgan.

The claims relate to conduct at JPMorgan and at Bear Stearns and Washington Mutual, which JPMorgan purchased in 2008. At issue are allegations that the firms sold risky mortgages and mortgage securities while misrepresenting their quality.

Among the purchasers were Fannie Mae and Freddie Mac, the government-backed housing finance giants that required a massive bailout in 2008 when their housing investments soured.

The deal was announced by the Federal Housing Finance Agency, which has overseen Fannie and Freddie since their 2008 rescue.

Agency head Edward DeMarco said the accord "provides greater certainty in the marketplace and is in line with our responsibility for preserving and conserving Fannie Mae's and Freddie Mac's assets on behalf of taxpayers."

"This is a significant step as the government and JPMorgan Chase move to address outstanding mortgage-related issues," DeMarco said.

The firm reached the agreement without admitting or denying wrongdoing.

Related: More banks in crosshairs

JPMorgan will pay $4 billion to resolve claims related to the alleged misrepresentation of mortgage-backed securities -- investment products created by bundling payments from individual loans.

It will also repurchase $1.1 billion worth of mortgages sold to Fannie and Freddie between 2000 and 2008 that the firms say do not meet their quality standards.

JPMorgan (JPM)said the settlements "are an important step towards a broader resolution of the firm's [mortgage-backed-securities]-related matters with governmental entities, and reflect significant efforts by the Department of Justice and other federal and state governmental agencies."

JPMorgan acquired Washington Mutual in 2008 after the failed bank had been taken over by the Federal Deposit Insurance Corporation. It's unclear whether JPMorgan will be able to pursue reimbursement claims with the FDIC for the portion of the settlement related to WaMu.

This issue has been a point of contention in JPMorgan's negotiations with the Justice Department, which wants to prevent the bank from passing on any settlement costs.

Securities sold by WaMu accounted for roughly $1.15 billion worth of the FHFA settlement.

Related: Half of nation's foreclosed homes still occupied

Investors initially shrugged off the news, which has been rumored for weeks. JPMorgan shares were up slightly in after-hours trading Friday, and have gained 20% so far this year.

JPMorgan is just one of 18 banks sued by the FHFA back in 2011 over the alleged misrepresentation of mortgage-backed securities, and is only the fourth to reach a settlement.

UBS (UBS) agreed to a settlement with the FHFA in July for $885 million. The agency has also settled with Citigroup (C) and General Electric (GE) for undisclosed sums.

JPMorgan is large enough to easily absorb the settlement costs. It's the biggest bank in the nation, with assets of $2.5 trillion and net income of $21.3 billion in 2012.

The bank has been buffeted by legal problems in the past few months, however.

It has paid over $1 billion in fines in connection with last year's "London Whale" trading debacle, and $80 million more over its allegedly unfair credit card billing practices.

In July, the bank agreed to pay $410 million to settle charges that it manipulated electricity prices in California and the Midwest. It is also facing scrutiny over its hiring practices in China and its alleged involvement in the Libor rate-fixing scandal.

JPMorgan posted a loss for the third quarter based on its massive legal expenses. CEO Jamie Dimon called the loss "painful" and warned that litigation costs could continue to be a drag on earnings for several quarters.