It's triggering your immune system to produce and/or store things, the presence of which then confers resistance when subsequently exposed to the actual pathogen.
So from that perspective, the next question is, "Do naturally occurring pathogens, presented in a natural manner, produce an optimal triggering themselves?" To which the answer, as you might guess from the messiness of real-world biology, is "No."
It turns out that while amazing our immune systems are also general purpose and adaptable, and such systems can get confused, or go off on a tangent, or focus on the wrong thing, etc etc.
Vaccines interact with this system by balancing two things: (1) how do I trigger the strongest, purest immune response that's effective on the thing I want to immunize against & (2) how do I avoid triggering such a strong immune response that I convince the body to attack itself?
It turns out, with modern biology, we actually can do better than nature on (1). Even if (2) still requires some guesswork (and a lot of testing).