You are talking about the Alice and Bob situations.
Alice has a pair of keys. Private and public.
Bob has a pair of keys private and public.
Bob and Alice swap public keys.
Alice uses bobs public key to encrypt a message, when Bob receives the message he uses his private key to decrypt the message.
The private keys that do the decryption are never swapped. Only the public key are swapped which allow encryption.
If some else has bobs public key, they can not decrypt a message because the key they intercepted is used for encryption.
There is also another method called a Diffie-Hellman exchange. Where Bob and allice still swap public keys but Bob uses his private key and allices public key to do a maths equation to get a secret. Allice will use her private key and bobs public key doing the same math equations will result in the same number.
So the secret was never shared between them they both did the same equation and came to the same result.