The mechanism responsible for the trick is the same as with walljump.
You jump towards the wall so that when Mario starts falling, his feet gain a support in the wall (such support is found every 16 pixels in the vertical direction).
When you do the jump in
ducking mode, you can stop ducking as soon as Mario gains the foothold in the wall. The result of this is that now Mario's head is overlapping with the ceiling.
This is enough to prevent him from being ejected from the wall to the left, and he will be ejected to the right instead.
As demonstrated in the GIF animation, when Mario is ducking (equivalent to small Mario), he'll be ejected to the left, and when he's standing, he'll be ejected to the right. This is due to the close proximity of the left edge of the wall.
When he's standing, the collision of his head dominates the test, and because his head is not close to a left edge, he'll be ejected to the right (no need to steer in either direction).
Whichever direction you're facing has no effect to this trick.
The whole point of this all is to gain access to the pipes before the screen has fully scrolled to the right.
Hope this helps.