The B button is primarily used to keep track of which sword Link has equipped, but it also has other functions throughout the game. This leads to various oddities and exploits regarding swords.
There are three ways the game keeps track of equipped swords.
There are four sword items in the game:
The game will check for one of these items on the B button whenever the B button attempts to reset itself. If it does not find one of these items, the Temp B value may be restored to the B button. If you have a sword item on B however, the B button will not be affected. See Minigame Items on B for more info.
The Swordless Flag records whether or not the player has equipped a sword before. When you begin the game, this flag is set. The flag will only be cleared when you manually press A to equip a sword from the equipment screen. Although traveling through time might put a sword on the B button, it does not affect the state of the Swordless Flag. Entering the Ganon battle will also set the Swordless Flag.
This flag primarily exists to determine whether to restore Temp B onto the B button in certain situations.
When the Swordless Flag is set, dying without a sword item on B will restore Temp B unless it is a value of 0. If Temp B is a value of 0, it will make the B button blank instead. This is because you begin the game without a sword item on B, but Temp B should not be restored at this point since it would be a value of 0. This check will also be performed anytime the B button attempts a reset.
Once the Swordless Flag has been cleared, Temp B will almost always be restored whenever the B button resets with a non-sword item on B. However, if the B button is blank it will become a Deku Stick instead. See Minigame Items on B for more info.
The Swordless Flag can also have many other side effects when the B button is blank. See Swordless Link for more info.
Sword slots are the three inventory slots on the equipment screen that store the three swords you can have at one time.
Only one sword slot can be highlighted at a time or none at all. Although the highlighted sword slot is used to indicate which sword is currently equipped, it's possible to have a different sword on B than the one that is highlighted. It's also possible to have a sword item on B without any sword slot highlighted.
If you have a non-sword item on B while one of the sword slots is highlighted, a strange effect will occur if you savewarp. When you load the file, the item in the previously highlighted sword slot will be reversed. If you had the highlighted sword, it will be deleted, and if you didn't have it, it will be placed in your inventory. This can be used to easily get the Kokiri Sword in the inventory again after it has been deleted.
Entering the Ganon battle will also reverse the bit for the Master Sword slot. If you have the Master Sword in the inventory when you enter the battle, it will be deleted. But if you don't have it, it will be placed in the inventory during the battle instead. It will still be removed from the B button, however.
Additionally, dying or failing the collapse timer in any room in the sequence after Ganondorf's death will always put Master Sword on the B button and highlight the slot for it, even as child Link. The reason for this is you might lose your sword during the Ganon fight, so it is always placed on B after death during the end sequence of the game.
Going forward in time will always put the Master Sword in the inventory, and Adult Reset will always place it on B and highlight the slot. On v1.1 and v1.2, savewarping as adult Link without the Master Sword in the inventory will always put Master Sword in the inventory, on the B button, and highlight the slot.