U.S. stocks closed higher on Wednesday but were down from earlier highs as energy stocks declined and Greece's debt crisis showed no clear signs of resolution.

The benchmark S&P 500's energy sector was dragged down by the biggest slide in oil prices since April after traders were surprised by a report that showed U.S. crude stockpiles rose for the first time in more than two months.

Euro zone central bank chiefs kept in place their cap on a funding lifeline to Greece, maintaining pressure as Greece's lenders run out of cash. Investor hopes for a Greek debt deal, which earlier had helped drive a rally, ebbed during the day, according to Michael Matousek, head trader at U.S. Global Investors Inc. in San Antonio.

"Until it's all said and done, they're just jockeying for position back and forth. The negotiation process still isn't over," Matousek said.

Many investors were also holding off ahead of Thursday's scheduled release of the closely watched U.S. non-farm payroll report for June while the three-day weekend celebrating the July 4th holiday kept others away, said Brian Fenske, head of sales trading at ITG in New York.

Stronger-than-expected jobs and construction data gave stocks some support as did Swiss insurance giant ACE Ltd.'s $28 billion offer for upmarket property insurer Chubb Corp.

The Dow Jones industrial average rose 138.4 points, or 0.79 percent, to 17,757.91, the S&P 500 gained 14.31 points, or 0.69 percent, to 2,077.42, and the Nasdaq Composite added 26.26 points, or 0.53 percent, to 5,013.12.

The S&P energy sector fell 1.3 percent, the only S&P sector in the red on Wednesday. U.S. crude oil futures settled down 4.2 percent at $56.96 a barrel.