There are several theories about oil formation, but the most accepted is that oil arose from organic decomposition and that occur under some specific conditions.
For the formation of the oil occurs the deposit and accumulation of organic materials in the seabed, originating the sedimentary basins. Over millions of years of action by microorganisms, pressure and heat transform organic material into a viscous by-product of carbon and hydrogen. This viscous material seeps into porous rocks and migrates to places with lower pressure and continues on course until it finds something that blocks it. Trapped, oil accumulates in the pores or crevices in the rocks, from where it occurs. The gas reserve is blocked at the top of the same rocky reservoir and the oil just below.
Natural gas, formed together with oil, is formed from the decomposition of organic materials that accumulate in rocks for thousands of years. In this decomposition process anaerobic microorganisms act.