After months of tense negotiations, the Justice Department on Tuesday finalized a record $13 billion settlement with JPMorgan Chase to resolve allegations that the bank knowingly sold faulty mortgage securities that contributed to the financial crisis.

This marks the largest penalty ever levied against a single company and represents a colossal win for the government after years of public criticism over its struggle to hold Wall Street accountable for crisis-era sins. It is also a tremendous black eye for a bank once lauded as the nation’s strongest financial institution to emerge from the crisis.

New York Attorney General Eric Schneiderman, who is a member of President Obama’s mortgage task force that helped negotiate the deal, announced the details of the settlement Tuesday.

The agreement puts to rest multiple state and federal probes into the JPMorgan’s sale of mortgage securities to investors. But it still leaves the bank and its executives at risk of criminal prosecution for fraud.

Like other banks, JPMorgan bundled hundreds of home loans into securities and marketed them as investments that could be traded like stocks. When homeowners defaulted on their mortgages in droves, the value of the securities plummeted and investors were saddled with huge losses. Government authorities subsequently launched probes into whether the banks misled investors about the risks and the quality of the securities.

“Without a doubt, the conduct uncovered in this investigation helped sow the seeds of the mortgage meltdown,” said Attorney General Eric H. Holder Jr., in a statement. “JPMorgan was not the only financial institution during this period to knowingly bundle toxic loans and sell them to unsuspecting investors, but that is no excuse for the firm’s behavior.”

The most complex component of the JPMorgan settlement involves $4 billion in aid directed to distressed homeowners. People close to the negotiations say Justice’s lead negotiator, Associate Attorney General Tony West, worked over the weekend with Department of Housing and Urban Development Secretary Shaun Donovan to hash out the terms.

Nearly half of the aid will go to reducing the principal amount JPMorgan customers owe on their mortgages. The bank will also be credited $300 million for temporarily suspending the collection of mortgage payments to increase the odds that borrowers will be able to remain in their homes.

The remaining money will be used to lower interest rates on existing loans as well as provide low-income borrowers new mortgages, which the bank is forbidden from selling to investors and forced to keep on its books to encourage responsible lending. The bank will also receive credit for curbing blight by demolishing foreclosed homes or absorbing the cost of homes it has not foreclosed on but are effectively abandoned by the homeowners.

An independent monitor will supervise the consumer relief portion to ensure it is completed by the end of 2016. If JPMorgan fails to spend the entire $4 billion before that deadline, it will have to pay an amount equal to the unexpended funds either to the government or to a nonprofit organization designated by the government.

Justice has earmarked $7 billion of the total settlement forinvestors who lost billions on the faulty securities. That amount includes a $4 billion deal announced last month by the Federal Housing Finance Agency, which regulates Fannie Mae and Freddie Mac. FHFA sued JPMorgan, along with 17 other firms, two years ago for misrepresenting the quality of the securities purchase by the mortgage-finance twins.

Another $1.4 billion will go to the National Credit Union Administration, a regulatory agency that sued JPMorgan to recover losses from securities sold to now-defunct credit unions. The remaining portion of the investor payout will be divvied up between the offices of the attorneys general of New York, Delaware, Illinois, Massachusetts and California.

At least $1 billion will go to resolve a lawsuit filed by Schneiderman in October 2012. About $400 million of that money is designated to help troubled homeowners, with another $613 million in cash to help families affected by Hurricane Sandy get housing counseling, among other things.

As a part of the agreement, the bank has to acknowledge that it, along with its affiliates Bear Stearns and Washington Mutual, made false representations to investors. JPMorgan also admits that its employees knew the loans in question did not comply with its own guidelines, but the bank allowed the loans to be bundled and sold anyway.

The bank has also agreed not ask the Federal Deposit Insurance Corp. to absorb the cost of soured securities tied to Washington Mutual, the failed bank that JPMorgan bought out of receivership for $1.9 billion in 2008.

JPMorgan had argued that the FDIC is responsible for Washington Mutual’s bad mortgage securities — some of which are a part of the Justice compliant. The standoff over the failed bank was a major sticking point during negotiations. In the end, JPMorgan reached a $515.4 million deal with the FDIC to bring the fight to a close.

The final piece of the landmark deal calls for JPMorgan to pay $2 billion in fines to Justice.

The bank cannot claim a tax deduction on the penalty, but it will be able to write off a portion of the consumer relief and investor compensation as an ordinary business expense The tax implications of the deal has raised concerns among lawmakers and advocacy groups in recent weeks.

Pulling off this historic settlement is a significant accomplishment for Attorney General Eric J. Holder Jr. He took a direct hand in the negotiations and, in an unusual move, held a 50-minute meeting with JPMorgan chief executive Jamie Dimon in late September.

Justice has levied multimillion-dollar fines against big banks, including HSBC and Barclays, but to lawmakers and consumer advocates, those penalties are tantamount to a slap on the wrist. Advocacy groups took Holder to task earlier this year for saying that some banks had become too big to prosecute.

But the JPMorgan settlement could quiet criticism against the department, especially as prosecutors plan to use the agreement as a template for reaching similar deals with other banks. Federal and state prosecutors, as part of the Obama administration’s mortgage task force, are looking into the sale, packaging and underwriting of home loans at nine other banks, including Bank of America, Wells Fargo and Citigroup.

“The size and scope of [the JPMorgan] resolution should send a clear signal that the Justice Department’s financial fraud investigations are far from over,” Holder said. “No firm, no matter how profitable, is above the law, and the passage of time is no shield from accountability.”

For JPMorgan, the settlement will help the bank shake off some of the vestiges of the financial crisis. Many of the investigations stem from Bear Stearns and Washington Mutual, ailing financial firms that JPMorgan acquired in 2008 at the height of the crisis. Dimon has said JPMorgan has already paid nearly $10 billion to unwind Bear Stearns’s troubled businesses and resolve litigation tied to the firm.

“We are pleased to have concluded this extensive agreement,” Dimon said in a statement. “Today’s settlement covers a very significant portion of legacy mortgage-backed securities-related issues for JPMorgan Chase, as well as Bear Stearns and Washington Mutual.”

A resolution with Justice reduces but does not end the legal headaches at JPMorgan. The financial goliath is also embroiled in federal probes into tactics used to collect credit card debts and its role in the ma­nipu­la­tion of a key interest-rate benchmark that affects trillions of dollars of bonds, among other matters.

The barrage of government investigations and multibillion-dollar settlements led JPMorgan on Oct. 11 to report its first loss in nearly 10 years. The bank suffered a net loss of $380 million after setting aside an additional $9.2 billion for future litigation expenses. All told, the bank has a $23 billion chest to cover its mounting legal costs.