With a light absorption up to 740 nm, BaNbO2N is promising for solar water splitting but normally owns a low activity due to a high defect concentration. Here, Mg is adopted as a dopant to modify the BaNbO2N. The presence of Mg not only reduces the defect concentration and enhances surface hydrophilicity but also tunes the bandgap as well as the band edge positions. These modifications greatly promote the charge separation and transfer of BaNbO2N, boosting the photocatalytic activities for O2-evolution reactions. An apparent quantum efficiency of 1.65 % at 420 ± 20 nm has been attained for Mg modified BaNbO2N. Overall water splitting at a stable gas-evolution rate (∼ 2.0 μmol·h−1 for H2 and ∼ 1.0 μmol·h−1 for O2) has been realized by integrating Mg-modified BaNbO2N into a Z-scheme system. These findings justify Mg as an effective dopant to modulate the photocatalytic behavior of Nb-based perovskite oxynitrides.