Apple surpassed Exxon Mobil as the stock markets’ most valuable company, although Exxon finished the day in the lead.

SAN FRANCISCO — For several hours on Tuesday, Apple surpassed Exxon Mobil, the energy company, as the stock market’s most valuable company.

But by trading’s close, Exxon Mobil was back in the lead, although less than $2 billion ahead of Apple, the maker of iPhones, iPads and Mac computers.

At the end of regular trading, Apple shares closed at $374.01, up 5.89 percent, for a market valuation of $346.74 billion. Exxon Mobil closed at $71.64, up 2.07 percent, with a market capitalization of only $1.58 billion more, $348.32 billion. During the day’s seesaw trading, Apple’s market cap surpassed Exxon Mobil by as much as $7 billion.

Analysts say they expect Apple and Exxon Mobil to continue jockeying for the top slot while the markets remain volatile. Apple peaked at $403.41 a share on July 26. But Exxon, which began sliding before the most recent market turmoil, hit a 52-week high of $87.98 on April 29.

The change in market valuation has more to do with the falling price of oil than with the popularity of Apple’s electronics. “Clearly the oil market has declined substantially over the past 10 days due to the debt crisis and the cost of oil,” said Andrew Lipow, a former Amoco trader who is president of his own Houston consulting firm. “However, we’re sure to see Exxon bounce back and forth” Mr. Lipow said that if the price of oil bounced back, Exxon stock would firmly reclaim the top spot.

Charles S. Golvin, an analyst specializing in mobile technology for Forrester Research, said: “For Apple to continue its success they will need to leap ahead of their own innovation and design approach before their competitors, including Google and Amazon, close the gap on smartphones and tablets.”