Every classical inner function $\varphi$ in the unit disk gives rise to a certain factorization of functions in Hardy spaces. This factorization, which we call the generalized Riesz factorization, coincides with the classical Riesz factorization when $\varphi (z)\,=\,z$. In this paper we prove several results about the generalized Riesz factorization, and we apply this factorization theory to obtain a new description of the commutant of analytic Toeplitz operators with inner symbols on a Hardy space. We also discuss several related issues in the context of the Bergman space.