The purpose of this paper is to show how to solve in Hamiltonian formalism the equations of the polar motion of any arbitrarily shaped elastic celestial body, i.e. the motion of its rotation axis (or angular momentum) with respect to its figure axis. With this aim, we deduce from canonical equations related to the rotational Hamiltonian of the body, the analytical solution for its free polar motion which depends both on the elasticity and on its moments of inertia. In particular, we study the influence of the phase angle δ, responsible for the dissipation, on the damping of the polar motion. In order to validate our analytical equations, we show that, to first order, they are in complete agreement with those obtained from the classical Liouville equations. Then we adapt our calculations to the real data obtained from the polar motion of the Earth (polhody). For that purpose, we characterize precisely the differences in radius J − χ and in anglel − θ between the polar coordinates (χ, θ )a nd (J, l) representing respectively the motion of the axis of rotation of the Earth and the motion of its angular momentum axis, with respect to an Earth-fixed reference frame, after showing the influence of the choice of the origin on these coordinates, and on the determination of the Chandler period as well. Then we show that the phase lag δ responsible for the damping for the selected time interval, between Feb. 1982 and Apr. 1990, might be of the order of δ ≈ 6 ◦ , according to a numerical integration starting from our analytical equations. Moreover, we emphasize the presence in our calculations for both χ and θ, of an oscillation with ap eriodTChandler/2, due to the triaxial shape of our planet, and generally not taken into account. In a last step, we apply our analytical formulation to the polar motion of Mars, thus showing the high dependence of its damping on the poorly known value of its Love number k. Moreover we emphasize the large oscillations of Mars' polar motion due to the triaxiality of this planet.