Roughly 40,000 homes purchased in the last five years in New York City are now underwater — worth less than what is owed on their mortgages — up sharply over the past year, according to a real estate market information company.

In fact, in Queens, the second-hardest hit of the five boroughs, nearly one in four of the 71,911 one-family, co-op and condo sales made in 2004-2009 is now underwater, according to Zillow.com.

The 23.3 percent of recently purchased Queens homes being underwater in the first quarter compares to 15.9 percent one year earlier. It shows that New York’s foreclosure pain may be far from over.

“Negative equity is highly correlated with foreclosure rates,” said Dr. Stan Humphries, a Zillow vice president. “Homeowners in negative equity are more vulnerable to economic shocks — loss of job, financial shock at the household level — and less able to navigate those shocks.”

According to an analysis of NYC real estate prices done for The Post by Zillow.com, in Manhattan 6.4 percent of homes purchased in the last five years are now underwater; in Brooklyn, the total is 14.4 percent; in the Bronx, levels rose to 26.3 percent from a year earlier; and, on Staten Island, 18.6 percent of recently-purchased homes are now underwater.

At the same time, New Yorkers who have purchased foreclosed houses at discount prices are also seeing their dream homes’ value decline.

Among Brooklyn, Manhattan and Queens foreclosed homes sold at auction from 2006-08, 32 percent in Queens and 35 percent in Brooklyn declined in value, according to an analysis by the real estate data site Propertyshark.com for The Post.

“It’s incredible,” said Bill Staniford, CEO of Propertyshark.com. “It shows the market has been getting significantly weaker, if you purchased property at a discount two years ago you are still losing money on it.”

“A recession, unemployment [expected] to rise, and a lot of debts taken on by homeowners over the last five years sets the stage for more foreclosure activity,” said Jonathan Miller, president/CEO of Manhattan-based real estate appraiser Miller Samuel Inc.