How switch will learn MAC in these situations?
Solution 1:
Switches don't send packets to discover the MAC addresses, they listen to the traffic and in that way they create a table relating the MAC address with the port from where the frame came.
In the cases that you show, Spanning Tree Protocol enters in work blocking one of the links to avoid a closed loop.
In both cases, each switch using Spanning Tree and through a process of discovery and negotiation, blocks the redundant links so each switch learns remote MAC addresses through one link only. How STP chooses the 'best' port to use is described on Wikipedia.
(Note that STP doesn't choose a best path for each individual MAC – it decides on a single topology globally. In contrast, the newer TRILL and 802.1aq "Shortest Path Bridging" technologies do route each MAC individually.)
If STP is disabled, then you just get a loop and the network melts down.