Millions of homeowners who are behind on their mortgage payments would get added protections from foreclosure through the end of 2021 under a set of rules completed Monday by the Consumer Financial Protection Bureau.

Under the rules, mortgage lenders generally can’t foreclose on a home without first contacting homeowners to see if they qualify for a lower interest rate or some other loan change that makes it easier to repay.

“We want servicers and homeowners to be actively engaged in loss-mitigation throughout the summer and the fall so that we can get as many people as possible to a good outcome,” Diane Thompson, a senior adviser to the agency’s acting director, said on a call with reporters.

If a modification isn’t possible or a homeowner doesn’t respond—or the property is unoccupied—the foreclosure can proceed after the rules go into effect on Aug. 31.

The purpose of the rules, agency officials said, is to ensure that mortgage companies thoroughly process the large number of borrowers expected to exit from temporary pandemic-relief programs that have allowed homeowners to postpone monthly payments until the fall. CFPB officials say they want to prevent any avoidable foreclosures.

An existing foreclosure moratorium for borrowers with mortgages backed by the federal government currently expires July 31.

A preliminary version of the rules could have effectively forbidden any foreclosures until 2022. Agency officials said that proposal might have given some mortgage services a disincentive to help borrowers this year.

“As the nation shifts from the Covid-19 emergency to the economic recovery, we cannot be complacent about the dangers we still face,” CFPB Acting Director Dave Uejio, said.

Consumer groups praised Monday’s rules. Melissa Stegman, senior policy counsel at the Center for Responsible Lending, said the regulation “equips homeowners with key protections and helps to prevent unnecessary foreclosures.”

Approximately seven million homeowners took advantage of the so-called forbearance period, according to industry estimates, and many borrowers will simply have their missed payments tacked on to the end of their loans. Borrowers who enter into such plans can skip payments if they suffer a pandemic-related hardship but have to make them up later.

As of June 22, some 2.06 million borrowers were still in a payment forbearance program, the mortgage-data firm Black Knight Inc. reported Monday. Of those, about 575,000 borrowers are on plans that are expected to expire in September and October, according to Black Knight, presenting a challenge to mortgage companies that service the loans.

Write to Andrew Ackerman at andrew.ackerman@wsj.com