The price of a barrel rose following reports of a fall in US crude stocks.
Bank shares also rose amid increased expectation of an interest rate rise.
The Dow Jones Industrial Average closed up 112.58 points at 18,281.03, the S&P 500 index was 9.24 points higher at 2,159.73 while the tech-heavy Nasdaq rose 26.36 points to 5,316.02.
Brent crude went above $52 a barrel at one point, its highest since June.
Activity in the US services sector hit an 11-month high in September, an encouraging sign for economic growth which raised speculation that the Federal Reserve might raise interest rates before the end of the year.
Banks, which would be expected to benefit from a rise in rates, rose by 2% as a group, the biggest gain for any day in two months.
"We're taking a little victory lap today after the surprisingly good economic data," said Kim Forrest, senior equity research analyst at Fort Pitt Capital Group in Pittsburgh.
Shares in the social media website Twitter closed up 5.74% after a report in the Wall Street Journal said the website was expected to receive takeover bids this week.
Later in the day, an unconfirmed report from Reuters said Twitter had told potential buyers it wanted to conclude negotiations about selling itself by the time of its third-quarter earnings on 27 October.
"It looks like Walt Disney could be a candidate, Salesforce.com and probably Microsoft," Douglas McIntyre, from 24/7 Wall Street in New York, told the BBC .
Elsewhere, Google has been named as being another potential bidder.
Mr McIntyre said there were two reasons Twitter was putting itself up for sale. "One is that their number of users has plateaued at about 300 million, which is huge, and the other one is that they haven't been able to figure out a way to make money on those 300 million people, so they've had lots of swings at it and it hasn't worked," he said.
Last month Twitter's shares jumped more than 20% after a report by the US business news channel CNBC said the home of the 140-character tweet had received takeover approaches.
(Close): US stocks ended the day higher on Wednesday, with energy shares the big gainers as the oil price hit a near four month high.