In the case of transmissible diseases like smallpox, controlled exposure ends up being protective. It was widely practiced prior to development of modern vaccines.
Consider yogurt. Some ancient person decided to sample curdled milk in the intestine of a dead sheep, and subsequently figured out how to replicate that experience.