Oil edged higher on Thursday as gains in wider global markets offset rising energy inventories and slumping demand.

U.S. crude rose 15 cents to settle at $51.12 a barrel. Brent crude rose 2 cents to settle at $50.80 a barrel.

The number of U.S. workers filing new claims for unemployment aid fell unexpectedly last week, suggesting the pace of layoffs was easing, helping to lift the U.S. stock market ahead of the settlement in oil prices. U.S. stocks slipped in later afternoon activity.

European equities rose on Thursday on strong company earnings and optimism the market was bottoming out.

The upbeat stock market has been providing the support lately, said Tom Bentz, senior commodity analyst for BNP Paribas Commodity Futures in New York.

We're drowning in oil and the market ignores that fact and prefers to focus on the possibility that the economy is recovering.

Oil prices have tumbled from record peaks over $147 a barrel hit in July, dragged down as demand slows and inventories rise due to the economic crisis.

U.S. oil demand in February fell to the lowest level for the month in 11 years, according to data from the Energy Information Administration, down 5.4 percent compared with year-ago levels.

On Wednesday, the EIA reported U.S. oil inventories rose to a fresh 19-year high last week. Gasoline stocks, however, showed a surprisingly steep 4.7-million-barrel drop ahead of U.S. summer driving season.

Markets were also eyeing the flu outbreak as officials urged increased worldwide precautions against an imminent pandemic and Mexico began shutting down parts of its economy to slow the spread of the new flu strain.

Analysts have said the flu could hurt jet fuel demand by curtailing travel.

Swine flu has become almost a sideshow for the oil market -- it might impact demand in specific areas like jet fuel, but if we can confirm the overall economy has bottomed, prices should slowly grind higher said Paul Harris, analyst at Bank of Ireland.

Iraq's oil minister Hussain al-Sharistani said world oil markets seemed to be coming into balance, but did not rule out a further cut in oil supplies by the Organization of the Petroleum Exporting Countries at its next meeting on May 28.

(Additional reporting Robert Gibbons in New York, David Sheppard in London and Jonathan Leff and Maryelle Demongeot in Singapore; Editing by Christian Wiessner)