It's hard to explain. There is a frame window for you to escape. It has a beginning (when hitstun wears off) and an end (when you get hit by the next move). In theory you could input Up B any time during this frame window. However, you don't know exactly when the beginning is. What you're afraid of with mashing is inputting once just before that window and once just after (as it may be very tiny). But in fact you know exactly when the end of the frame window is (ie just before getting hit). So by inputting it on the last frame possible before getting hit, it's the only time you can be certain that you actually are within that frame window. Therefore, it's the only time you can reliably time your press, independently of how tiny the frame window may be (as long as it's not a combo obviously).
By the way, I should have added in my first post "but it never works for me". I still believe this is the best solution.