We have investigated the formation of inversion domain boundaries in Al0.13Ga0.87N layers grown on sapphire substrates by metalorganic chemical vapor deposition using transmission electron microscopy. By increasing the Mg source flow rate, the reduction of dislocation density occurred up to the Mg source flow rate of 0.103 μmol/min. While the vertical type inversion domain boundaries (IDBs) were observed in the Al0.13Ga0.87N layers grown with the low Mg source flow rate, the IDBs in the Al0.13Ga0.87N layers grown with the high Mg source flow rate have horizontally multifaceted shapes. The change of polarity by the IDBs of horizontal type also resulted in the 180°rotation of pyramidal defects within the same AlGaN layer. Therefore, We found that the Mg source flow rate affects significantly the dislocation density, the type of IDBs, and the shape of pyramidal defects in AlGaN layers.