Morphine and endorphins have different molecule substance but have a similar shape that they are able to bind on the same receptor. You can say they both agonist for the opioid receptor.
Since they can bind the opioid receptor, they both can cause the same effect even though they are a different molecule.