Red-hot demand for used cars is turning the auto-lending world upside down.

Prices are so high that some lenders are coming out ahead on defaulted auto debt. And far fewer borrowers are underwater on their car loans, meaning they don’t owe more than the car is worth when they trade it in. It is a tricky time to buy a car, but a pretty good time to owe money on one.

It is a silver lining of the surge in used-car prices. Americans are shopping for cars at near-record numbers, but a computer-chip shortage has starved dealers of inventory. The severe shortage of vehicles to sell, including previously owned ones, has cars acting more like houses—growing in value and delivering meaningful gains to their owners. The average price of a used car was $18,453 in June, up more than 34% from the same time last year, according to Manheim Inc.

About 20% of owners were underwater when trading in their cars in June, down from 32% a year earlier, according to car-shopping site Edmunds. Those who are underwater owe $3,848 on average, 25% less than underwater borrowers at the same time in 2020.

The shift is also welcome news for lenders, who typically don’t make enough selling the cars of defaulted borrowers to cover unpaid loan balances. Four large banks more than covered their losses on defaulted car loans in the second quarter, the first time they have reported such recoveries in at least two decades, according to Piper Sandler Cos.

When lenders repossess the cars of borrowers in default, they typically write off the unpaid loan balance, expecting to sell the vehicle for less than the remaining balance. Lenders report net recoveries when the total they recoup from selling used cars and collecting previously unpaid balances is greater than the total value of charged-off loans.

Ally Financial Inc., one of the country’s largest auto lenders, recouped $5 million on defaulted car loans in the second quarter, a first for the bank and a giant reversal from the $97 million in defaulted auto loans it charged off in the first quarter.

“It is a huge shift from where we’ve seen historic loss levels,” Ally Chief Financial Officer Jenn LaClair said.

JPMorgan Chase & Co., PNC Financial Services Group Inc. and U.S. Bancorp also reported net recoveries on defaulted auto loans between April and June. Each of the banks said its policy after selling a repossessed vehicle is to return any surplus funds above the unpaid loan balance to the borrower.

Inflated car prices aren’t entirely positive for lenders. When they issue new loans, they risk larger losses if prices decline to more-normal levels and a borrower defaults.

But borrowers, flush with government stimulus payments, are defaulting on auto loans in low numbers. The average charge-off rate, or the share of outstanding credit in default, at five large lenders was 0.14% in the second quarter, down from 0.5% in the first quarter and 0.7% during the same period in 2020, according to Piper Sandler.

The banks said high used-car prices have helped lower charge-off rates since more struggling borrowers can sell their vehicles for more than their loan balance before a repossession. And the solid financial standing of many consumers means that most have been able to keep up with their payments and pay off balances on previously defaulted loans.

“It’s astounding that you have so many consumers that are going to find themselves in favorable positions, where in the past so many people have fallen into the bucket of not being able to chip away at that debt,” said Ivan Drury, senior manager of insights at Edmunds.

Borrowers with negative equity at the time of trade-in tend to be offered higher interest rates, steeper monthly payments and longer terms, according to Edmunds. The increased costs mean a smaller portion of their monthly payments goes toward the principal balance in the initial years of a loan. For some borrowers, the result is a cycle in which each new trade-in leaves them deeper underwater.

Chad Simmons got $15,400 this summer for his 2018 Hyundai Ioniq—$2,900 more than his unpaid loan balance. He used the difference as a down payment on a 2020 Honda Civic with 4,000 miles. He had racked up about 60,000 miles on his 2018 model while traveling for work from his home in Troy, Ala.

“Every car I’ve ever had I’ve had negative equity,” Mr. Simmons said. “[Trading in] wasn’t even something I was considering.”

SHARE YOUR THOUGHTS

Have you tried to buy or sell a used car during the pandemic? Share your experience below.

His monthly payments are about the same as he paid for his older car, he said.

The share of borrowers with negative equity spiked during the early months of the pandemic alongside falling used-car prices. It fell below pre-pandemic levels in March 2021, shortly after used-car prices began their latest climb.

Lenders don’t expect the upside-down economics to last. Used-car prices will begin to decline over the next few months but at a slow enough pace for borrowers to continue to take advantage of positive equity, Mr. Drury said.

Write to Orla McCaffrey at orla.mccaffrey@wsj.com