Exxon Mobil rose 2.9%, and Chevron climbed 2.1% as the price for a barrel of benchmark U.S. oil rose 1.4% to settle at $62.02.
Stocks of smaller companies also did better than the rest of the market, with the Russell 2000 index rising 0.7%.
All told, the S&P 500 fell 37.14 points to 6,926.60. The Dow Jones Industrial Average dipped 42.36 to 49,149.63, and the Nasdaq composite fell 238.12 to 23,471.75.
Oil prices have rallied recently after protests swept Iran, which is a member of the OPEC group that helps set crude prices. The protests could lead to disruptions in production and squeeze supplies of crude.
Brent crude, the international standard, rose 1.6% and briefly brought its gain for the year so far to nearly 10%, before prices for both it and U.S. oil fell back later in the afternoon.