Because it primes the immune system for future exposures to virulent pathogen. Vaccination introduced attenuated (non-virulent) form of a pathogen into the body and allows the immune system to develop antibodies against the antigen. In case of future real exposure to the pathogen, the immune system will already have developed memory B and t cells against the pathogen hence will have an effective response.