We develop a structural model of the global market for crude oil that for the first time explicitly allows for shocks to the speculative demand for oil as well as shocks to flow demand and flow supply. The speculative component of the real price of oil is identified with the help of data on oil inventories. Our estimates rule out explanations of the 2003-2008 oil price surge based on unexpectedly diminishing oil supplies and based on speculative trading. Instead, this surge was caused by unexpected increases in world oil consumption driven by the global business cycle. There is evidence, however, that speculative demand shifts played an important role during earlier oil price shock episodes including 1979, 1986 and 1990. Our analysis implies that additional regulation of oil markets would not have prevented the 2003-2008 oil price surge. We also show that, even after accounting for the role of inventories in smoothing oil consumption, our estimate of the short-run price elasticity of oil demand is much higher than traditional estimates from dynamic models that do not account for for the endogeneity of the price of oil.