We should have a chemist weigh in, but the bottom line is that Oxygen is a product of fusion in stars that are far along their stellar cycles; usually stars that are about to explode in supernova due to the exhaustion of hydrogen to use in fusion.
The earth and its solar systems should be considered as the “leftovers” of an earlier star or stars; one super big formed almost completely out of hydrogen. Super massive stars make hydrogen into helium and helium into heavier atoms etc… until it takes more energy to form higher elements, I believe after Iron, and they explode under their own gravity, forming all the other elements.
These explosions leave all the elements in a big cloud. These clouds eventually begin to spin under the influence of gravity, into what is called an accretion disk. Oxygen has a charge that makes any nearby hydrogen stick to it, until it has two hydrogen atoms, forming a complete chemical bond, which we know as water. Hydrogen is the most common element in the universe.
The heavier material in said cloud will fall to the center (the sun, the inner planets). These clouds eventually collapse over time into planets. Those clouds near to the sun are hot, and water (frozen h20 dust bunnies) caught early in their development boils off to space.
Earth is at a distance from the sun where water dust bunnies can form a liquid if introduced.
After Mars, the early solar system was cool enough that Hydrogen and Oxygen would bind into asteroids, and these elements are light enough to avoid being sucked into the sun during the period when the solar system was a gas cloud.