Poisson and Hamiltonian structures of compartmental dynamical systems are reviewed, and their associated Casimir functions are shown to be useful in order to obtain solutions in closed form for this outstanding class of nonlinear dynamical systems. In particular, after sketching the essentials of the susceptible-infective-recovered (SIR) model, we make use of the Hamiltonian structure of a modified SIR system where recovered individuals are removed from the population, in order to obtain the exact analytical solution of this compartmental epidemiological model in closed form. In this dynamical system, the populations [ ] and [ ] of susceptible and recovered individuals are found to be generalized logistic functions, while infective ones [ ] are given by a generalized logistic function times an exponential, all of them with the same characteristic time. The dynamics of this modified SIR system is analyzed and the exact computation of some epidemiologically relevant quantities is performed. The main differences between this modified SIR model and the original SIR system are presented and explained in terms of the zeroes of their respective conserved quantities. Moreover, it is shown that the modified SIR model with time-dependent transmission rate can be also solved in closed form for certain realistic transmission rate functions.