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.