The purpose of the Subnet Mask is to determine whether the host you are trying to speak to is on your network or on a foreign network.
If the other host is on your network, the sending host is going to send the data directly to the other host.
If the other host is on a foreign network, the sending host is going to send the data to its default gateway.
In your example:
PC1: 192.168.1.10/24 PC2: 192.168.1.20/16
PC1 is configured as a
/24, which means it thinks everything in this range is on the local network:
192.168.1.0 - 192.168.1.255. This range includes PC2's IP address, and therefore PC1 considers PC2 to be on its local network.
PC2 is configured as a
/16, which means it thinks everything in this range is on the local network:
192.168.0.0 - 192.168.255.255. This range includes PC1's IP address, and therefore PC2 considers PC1 to be on its local network.
Since they both think the other is on the local network, they both follow the rules to communicate with another host on the local network. The fact that their masks are not identical ends up being irrelevant.
That said, as Xavier Nicollet pointed out in the comments... just because it works, doesn't mean it's a good idea. This should be avoided when possible.
Disclaimer: The Youtube video links above are my own creation, I am providing them to the reader to show additional context about hosts communicating on the same networks and foreign networks