In the split second after you hit A to grab the Master Sword, the Master Sword appears on the B button as a child. During this instant, it is possible to interrupt the sword pull cutscene with the Odd Mushroom timer expiring, which warps you back to Lost Woods with Master Sword on B.
Regardless of what version you play, swords on B stay when you quit, so you can keep it. However, the act of going back in time will always overwrite the Child B button with the Kokiri Sword, so you will only have it as long as you are still a child.
Only Master Sword is possible with this technique, because the game doesn't check what is supposed to be on Adult B button during that cutscene. It always equips Master Sword (even if you had a Bottle or another item there).
Replacing the Odd Mushroom with a Bottle via 21 Bottles while doing this glitch will teleport Link back to the Temple of Time instead of the Lost Woods.
When your in the Ganon's Castle Tower Collapse sequence, the Master Sword is equipped to the B button when you respawn. This is to prevent you from becoming swordless if you die during the Ganon Battle before getting the sword back. You can also get the Master Sword as child through picking it up in the fight. If you get to the Tower Collapse sequence as child using Ganondoor or Farore's Wind wrong warp, you can simply die to equip the master sword on B.