  Within a systematic approach based on the dimensionally regularized
nonrelativistic quantum electrodynamics, we derive the complete result for the
two-loop correction to order $(\alpha/\pi)^2 (Z \alpha)^4$ for the $g$ factor
of an electron bound in an $nS$ state of a hydrogenlike ion. The results
obtained significantly improve the accuracy of the theoretical predictions for
the hydrogenlike carbon and oxygen ions and influence the value of the electron
mass inferred from $g$ factor measurements.
