Just like a lot of other weird US/English units.
The oil barrel unit came from 1800s wooden barrels which were standardized to carry 42 gallons. Today a modern 55 gallon drum would contain 1.3 "barrels" of oil.
The measurement originated in the early Pennsylvania oil fields. In the early 1860s, when oil production began, there was no standard container for oil, so oil and petroleum products were stored and transported in barrels of different shapes and sizes for beer, fish, molasses, turpentine, etc. Both the 42-US gallon barrels (based on the old English wine measure), the tierce (159 litres) and the 40-U.S.-gallon (151.4-litre) whiskey barrels were used. 45-gallon barrels were also in common use. The 40-gallon whiskey barrel was the most common size used by early oil producers, since they were readily available at the time.<7>
The origins of the 42-gallon oil barrel are obscure, but some historical documents indicate that around 1866 early oil producers in Pennsylvania came to the conclusion that shipping oil in a variety of different containers was causing buyer distrust. They decided they needed a standard unit of measure to convince buyers that they were getting a fair volume for their money. They agreed to base this measure on the more-or-less standard 40-gallon whiskey barrel, but added an additional two gallons to ensure that any measurement errors would always be in the buyer's favor as an additional way of assuring buyer confidence, apparently on the same principle as is behind the baker's dozen and some other long units of measure. By 1872 the standard oil barrel was firmly established as 42 US gallons.
A "barrel" of oil today has no meaning other than 42 gallons. Most oil never makes it to a barrel. Oil rigs are connected directly to pipeline networks and oil spends its whole life in pipes or supertankers. Still the unit kinda stuck.