Magnetic phase transition in coherently coupled Bose gases in optical lattices