Mass action law versus local contagion dynamics. A mean-field statistical approach with application to the theory of epidemics