Maryland Gov. Martin O’Malley signed a bill into law Thursday that reduces the time within which debt collectors can pursue homeowners in court for old fore­closure debt from 12 years to three.

The law moves Maryland — which has the second-highest foreclosure rate in the nation, according to RealtyTrac — from among those states with the longest time frames for mortgage debt collection to the middle of the pack.

“We had essentially the worst law in the country,” said state Sen. Jamie B. Raskin. “And that just adds insult to injury. America is the land of second chances,” added Raskin (D-Montgomery), who introduced the measure in January with Del. Stephen W. Lafferty (D-Baltimore County).

Advocates and lawmakers say the shorter time frame will help homeowners who rebuild their lives after foreclosure and could be forced into bankruptcy by old debts.

The action by O’Malley (D) follows a Washington Post investigation in June that examined how homeowners in Maryland were increasingly being taken to court for mortgage debt years after foreclosures had been completed. For example, a property with a $500,000 mortgage that is worth $300,000 when it is foreclosed on leaves a $200,000 debt, or what is commonly referred to as the “underwater amount.”

Maryland’s new law, which takes effect July 1, will affect 214,000 homeowners who are underwater on their mortgages in the event of foreclosure, said Marceline White, executive director of the Maryland Consumer Rights Coalition.

The law is also retroactive and will apply to those whose homes were foreclosed upon during the 2008 housing crisis if the properties were owner-occupied at the time.

The measure passed 47 to 0 in the Maryland Senate and by a wide margin, 102 to 33, in the House of Delegates.

The bill, which first sought to reduce the time frame lenders had to file the claim in court to 180 days, was initially opposed by the Maryland Bankers Association. That provision was later extended to three years.

“The association no longer opposes the legislation,” said Kathleen Murphy, the association’s president and chief executive, “because [the three-year time frame] codified the existing practices that lenders used in Maryland.”

White said the law does not address the interest that accumulates on that debt after foreclosure. In some cases, it can add up to tens of thousands of dollars.

For example, in 2013, a Montgomery County homeowner was ordered to pay $115,000 plus nearly $23,000 in interest on a foreclosure that occurred years earlier.

“There may be more things we need to do in looking at the interest,” White said.

White said that the fear among advocates was that a broader bill might not pass. Raskin agreed.

“We had to take it one step at a time so that the bill was not loaded down with so much weight that it might collapse,” Raskin said.

Before the new legislation, Maryland’s state statute restricted lenders to a three-year time frame to pursue mortgage debt — but a little-known exemption extended that to 12 years.

In addition to the period allotted to file a claim in court, debt collectors had an additional 12 years to collect the debt and a one-time renewal of 12 years — for a total of 36 years to pursue homeowners.

Mortgage debt can be collected in 40 of 50 states nationwide, including the District, Virginia and Maryland, as well as every state along the East Coast. The time to take legal action to collect varies widely, from 30 days to 20 years.

In Connecticut and Georgia, debt collectors have just a month. In New York and New Jersey, it is three months.

But in states such as Rhode Island and New Hampshire, the ceiling is 20 years. That period is just to file a claim and does not include the years tacked on to collect the money.

Once a judgment is entered in neighboring states, such as Virginia, lenders have 20 years to collect. In the District, it’s 12 years. These time frames can also be extended through renewal.

“Consumer advocates believe 12 years is a very long time for a bank to pursue a debt, especially for someone who’s lost their home,” White said.