By Nicole Friedman

Home sales surged in July, signaling how much the pandemic is reshaping where and how Americans want to live during this period of social distancing and working from home.

Home buyers who were reluctant to venture out in March and April when much of the country was under lockdown have returned in force since late spring. With the effects of coronavirus showing little signs of abating, many home shoppers have new priorities for a place to live, or are accelerating existing plans.

Buyers are ready to move farther from cities, now that many workers aren't commuting every day. The pandemic has spurred some households to live closer to family, or somewhere that offers more space with so much time spent at home, brokers and economists say.

"People that were in condominiums are looking for townhomes, and people in townhomes are looking for single-families," said Bob Chew, a group leader at Berkshire Hathaway HomeServices PenFed Realty in Ellicott City, Md. "People are at home, and the more time they spend in the home, they realize, 'I want some different features in my home.'"

The July sales numbers were among the strongest the housing market has ever seen. Sales of previously owned homes jumped 24.7% from a month earlier to a seasonally adjusted annual rate of 5.86 million, according to the National Association of Realtors on Friday. That was the strongest monthly gain ever recorded, going back to 1968. It was also the highest sales pace since December 2006.

Mortgage rates that hovered near all-time lows helped the housing market break out of a long slump heading into 2020. Now, after a sharp decline in the early spring due to the coronavirus outbreak, pent-up demand is pushing the market even higher. Homes typically go under contract a month or two before closing, so July figures largely reflect purchase decisions made in May or June.

"The housing market is actually past the recovery phase and is now in a booming stage," said Lawrence Yun, chief economist at NAR. "New demand has been created because of the pandemic, with the work-from-home flexibility."

First-time buyers accounted for 34% of sales in July, NAR said, a category that includes many millennial buyers.

This group, who range from their mid-20s to their late 30s, are a growing presence in the housing market. Older millennials who delayed getting married and having children are now reaching those life milestones, which increases homeownership demand. Younger millennials, who are now entering their 30s, are starting to buy homes more actively at an age when previous generations also began homeownership.

The housing market offers one of the few signs of strength in the U.S. economy, which has sputtered under the pandemic. Housing starts soared 22.6% in July from June, well above expectations. New-home sales, which make up about 10% of the market, have also roared back this summer.

Home sales can have positive knock-on effects for the economy, too, as consumers spend more on home goods and renovations.

The strong demand and a shortage of supply pushed the median home price to new highs, which could make houses less affordable for some first-time buyers. The median existing-home price rose 8.5% from a year earlier to $304,100, a record high nominally and adjusted for inflation, NAR said.

Some economists warn that the recession and continued high unemployment could dampen homebuying interest later on.

Still, buying surveys suggest demand looks strong in the near term. About 40% of home buyers polled by Realtor.com in June said they are looking to buy a home sooner because of Covid-19, while only 15% said the pandemic slowed down their timeline. ( News Corp, parent of The Wall Street Journal, operates Realtor.com.)

Brittani Baynard and Sam Krueger decided to spend the money they'd saved for their wedding to buy a four-bedroom home in Madison, Wis., in June. They had planned to buy a house in the next two years, but the pandemic made them want to move sooner out of their apartment into a house with more space for Ms. Baynard's 8-year-old daughter, she said.

Fearing another phase of the pandemic, Ms. Baynard said, "I want us all to be comfortable."

In Billings, Mont., buyers are coming from as far away as Texas and New York. Some are buying houses without seeing them in person, said Deb Parker, president of the Billings Association of Realtors.

"It makes for a very competitive market, that's for sure," she said. "If they can sell someplace where the values are much higher, they can come in and pay cash for a house in Montana."

Compared with the last time sales were this high, in 2006, lending standards are tighter and the supply of homes for sale is much lower, Mr. Yun said.

Demand is so robust that 68% of the houses that sold in July were on the market for less than a month, NAR said. Brokerage Redfin Corp. said more than half of its offers in July faced at least one competing bid.

In many cities, agents say inventory can barely keep pace with demand. There were 1.5 million homes for sale at the end of July, down 21.1% from July 2019, according to NAR.

Rachel Crawford and her husband experienced how tight supply is firsthand. They had already planned to move to a different state to be closer to family. "The pandemic was actually sort of an extra motivator, " Ms. Crawford said.

After they listed their house in Fall City, Wash., in July, it received multiple offers and sold within a month. They then had to make offers on multiple homes in Parker, Colo., before they had one accepted.

Write to Nicole Friedman at nicole.friedman@wsj.com