U.S. oil prices closed at their lowest in more than six years on Wednesday after U.S. data showed an unexpected rise in crude stockpiles.

U.S. crude stocks were up 2.6 million barrels at 456.21 million barrels last week, the U.S. Energy Information Association said. Stocks at Cushing, Oklahoma, the delivery point for U.S. crude futures, rose 326,000 barrels to 57.44 million.

U.S. crude oil futures, also known as West Texas Intermediate (WTI), settled down 4.3 percent, at $40.80 a barrel. That was the contract's lowest close since March 2009. The front month, September, U.S. contract expires on Thursday.

was down 20 cents, at $46.90 a barrel.

This was in sharp contrast to a Reuters poll, which forecast a 777,000-barrel inventory decrease.

Read MoreEPA proposes new standards to cut methane emissions



"The numbers were a total surprise, with crude showing a build when the whole (market) was forecasting a draw," Tariq Zahir, managing member at Tyche Capital Advisors in Laurel Hollow in New York, said.

"We feel these numbers are not factored in to the current price of spot crude."