U.S. oil prices ended higher Thursday, snapping what would have been the longest losing streak in 30 years.

Light, sweet crude for August delivery rose for the first time in 10 sessions, ending 64 cents, or 0.6%, higher at $102.93 a barrel on the New York Mercantile Exchange. Had the U.S. benchmark oil contract ended lower for a 10th session, it would have been the longest period of consecutive declines since July 1984.

With prices near two-month lows, traders who had bet on falling prices made purchases Thursday to close those positions.

"The bears have had a good run and are looking to lock in profits and take some cash off the table," said Stephen Schork, editor of the Schork Report in Villanova, Pa.

Still, the negative factors that weighed on U.S. oil prices remain. U.S. crude-oil production last week reached more than 8.5 million barrels a day for the first time since October 1986, while crude processing by refineries are at a nine-year high, according to a note by Commerzbank.

"The bottom line is that we're sitting on a tremendous amount of oil at the moment," Mr. Schork said. "We're at a point in the summer where demand for crude oil has already peaked."

Brent crude for August delivery on the ICE Futures Europe exchange gained 39 cents, or 0.4%, to $108.67 a barrel, snapping an eight-day losing streak. It was the largest one-day dollar and percentage gain in three weeks.

Gasoline futures ended higher, with the August contract up 0.7% at $2.9576 a gallon. August diesel rose 0.6% to $2.9199 a gallon.