An x-ray analysis of MgB2-based materials shows that they contain MgB2 and MgO phases. According to a quantitative Auger analysis (taken after removing the oxidized surface layer by Ar ion etching in the microscope chamber) the MgB2 phase contains some amount of oxygen that approximately corresponds to the composition MgB2.2-1.7O0.4-0.6. Rietveld refinement of the MgB2 phase, based on EDX data with varying B/O content, leads to the composition MgB1.68-1.8O0.2-0.32. Ab-initio modelling of boron substitution by oxygen in MgB2 ([Delta]Hf =-150.6 meV/atom) shows that this is energetically favourable up to the composition MgB1.75O0.25 ([Delta]Hf =-191.4 meV/atom). In contrast to carbon substitution, where very small levels of doping can dramatically affect the superconducting characteristics of the material with concomitant changes in the electron density, oxygen substitution results in very little change in the superconducting properties of MgB2. The formation of vacancies at the Mg site of both MgB2 and substituted MgB1.75O0.25 was modelled as well, but has shown that such processes are energetically disadvantageous ([Delta]Hf of Mg0.875B2 and Mg0.75B1.75O0.25 are equal to -45.5 and -93.5 meV/atom, respectively).