A vaccine is effective by triggering a small immune reaction within your body. They are essentially very small amounts of X virus, so that your body can trample it and build an immunity to the real thing.
I think the question the OP was trying to ask can be rephrased as "why does a random piece of protein trigger an immune response?"
I mean the short version is "everything does". The human immune system is psychotic to the point that we have organs it has to be kept isolated from or it will destroy them.