The variational principle for stars with a phase transition has been investigated. The term outside the integral in the expression for the second variation of the total energy of a star is shown to be obtained by passage to the limit from the integration over the region of mixed states in the star. The form of the trial functions ensuring this passage has been found. All of the results have been generalized to the case where general relativity is applicable. The known criteria for the dynamical stability of a star when a new phase appears at its center are shown to follow automatically from the variational principle. Numerical calculations of hydrostatically equilibrium models for hybrid stars with a phase transition have been performed. The form of the trial functions for the second variation of the total energy of a star that describes almost exactly the stability boundaries of such stellar models is proposed.