In this study, the influence mechanism of rare earth elements Ce and Nd on the corrosion behavior of Mg alloy was studied by combining experimental characterization with the first-principles calculation. The changes of work function and surface energy of the rare Earth phase and matrix phase in binary rare Earth Mg alloys Mg-0.3%Ce and Mg-0.3%Nd. In addition, the electronic properties of the second rare Earth phase and matrix phase, and the correlation between the interface energy of the second rare Earth phase and matrix and the corrosion behavior of Mg alloys were investigated. The experimental results show that Mg12Ce and Mg12Nd as the second phases of Mg-0.3%Ce and Mg-0.3%Nd, respectively, have not obvious roles in the galvanic corrosion of the alloys. The corrosion morphology of the two alloys is similar, the matrix undergoes filamentary corrosion through the grain to a large area. The results of the first-principles calculation show that the binding energy of the rare Earth phase is higher than that of the matrix phase, this implied the addition of light rare Earth elements Ce and Nd can improve the stability of the second phase. The work function of different surfaces of the second phase is similar to that of the matrix phase, so limited effect of the rare Earth phase on corrosion behavior has been found. In addition, the addition of rare Earth Ce and Nd will decrease the surface stability of Mg matrix, this result reaffirms the higher corrosion rates of Mg-0.3%Ce and Mg-0.3%Nd over that of the pure Mg.