Buoyed by rising deliveries and growing margins, KB Home (KBH) revealed a narrower-than-expected second-quarter loss on Friday, sending the home builder’s shares surging.

Los Angeles-based KB Home said it lost $24.1 million, or 31 cents a share, last quarter, compared with a loss of $68.5 million, or 89 cents a share, a year earlier. Analysts had been bracing for a deeper loss of 34 cents a share.

Revenue jumped 11% to $302.9 million, narrowly topping the Street’s view of $301 million. Housing gross profit margins leaped to 16.9% from 7.3% the year before and 9.7% in the first quarter.

KB Home said it delivered 1,290 homes last quarter, up 2% from the year before. While deliveries slid in the West and Southwest, they jumped 13% and 5% in the Central and Southeast regions, respectively.

In another positive sign, average selling prices climbed 9% to $233 million year-over-year, highlighted by a 33% surge in the West.

Meanwhile, backlogs rose to 2,962 homes at the end of the quarter, up from 2,422 homes the year before.

“We have a strong backlog of homes with higher selling prices and better margins to help restore profitability, and we anticipate achieving further gains in our margin performance as our revenue growth and cost-management efforts take hold,” CEO Jeffrey Mezger said in a statement.

Mezger described the environment in the housing markets as an “improving, but uneven recovery.”

Wall Street cheered KB Home’s earnings, bidding the shares 9.2% higher to $8.70. The gains put the stock on track to add to its 2012 surge of nearly 30%.

Shares of some other home builders like Beazer Homes USA (BZH), PulteGroup (PHM) and Hovnanian (HOV) saw their shares rise as well.

Follow Matt Egan on Twitter @MattMEgan5