The pressure-induced phase transition from wurtzite to rocksalt in ZnO was investigated using first-principles lattice dynamics calculations within the quasi-harmonic approximation. Structural and thermodynamical properties at finite temperatures were well reproduced for both phases. The transition pressure shows negative temperature dependence inconsistent with previous experimental observations. This can be attributed to a greater increase in vibrational entropy of the rocksalt phase with temperature. The effect of alloying with MgO on the phase transition was also examined in conjunction with the cluster expansion and cluster variation method. The transition pressure decreases with an increase of MgO content. Below the solubility limit of MgO in ZnO, this behavior is due to the energetical preference of the rocksalt phase to the wurtzite phase in MgO. Above the solubility limit, the increase in configurational entropy by the transition from a wurtzite ZnO-rocksalt MgO mixed phase to a rocksalt single phase makes a dominant effect.