If you sat on the couch all day and drank 10000 calories of whey protein shakes a day you'd still get fat.
Well, if it was NOTHING but protein, you
might end up in "rabbit starvation" (if such a thing exists for really reals) but since it would definitely have carbs and fats along with it you would, you say, avoid the mythic and epic death by rabbit starvation, and process along nicely into "fatitute" due to overall caloric intake.
Protein counts because you eat other things....
Trying to eat *only* protein leads to death when successful.
None of it is magic.
A wise man once said, "if human bodies somehow didn't use all the excess calories we feed them, fat people would be so rare we would have them on museum display." (paraphrased)
As to why you need a surplus to gain, the laws of thermodynamics govern that fairly well.
Same reason your car won't move forward without consuming enough fuel to produce energy in excess of that required to overcome inertia and perform work on mass.
If you have an alternate energy source (like fat guys do) you can gain while in a small
consumption deficit because you are pulling the rest of the energy from fat stores (there are limits here).