Yes endorphins are compounds which are the endogenous analogs to heroin, morphine etc. People feel good when they smoke opium or inject heroin and they feel good when the body secretes endorphins (typically in response to stress/injury). Its a lock and key analogy for sure, any chemical compound with the appropriate comformation will 'fit' into the cell receptor to initiate a cascade of intra and intercellular effects which eventually culminate in the 'feel good.'
It is important to note that feeling good is a subjective perception. Science can explain a lot of things but we still really have no clue how a bunch of chemicals attaching to cell receptors and electrical impulses zooming around the brain culminate in our awareness, perception, consciousness and so on. We can break down the Physics, (Bio-)Chemistry, Anatomy and Physiology, Pharmacology etc., of the structure of the chemical, how it may have an affinity for a certain receptor, or why it may have a certain conformation. We can even figure out what is really happening in the cell once the chemical 'fits' into the receptor. However we cannot really say how all of that stuff comes together into the 'human experience'/'feel good' perception we have.
Take any drug for instance. We pretty much know about their effects, what cells/receptors/neurotransmitters they may mimic or inhibit or what-have-you. But how does taking, say, LSD make you trip out? Why do many people feel good and 'see god,' while others have psychotic breakdowns or 'bad trips?' Or how about the interesting things that happen with mood without drug involvement? Ever had a bad day and be very grumpy, then you are in your car (or wherever) and hear a great song or a good joke and suddenly you 'feel good/better?' Maybe you start laughing until you have tears coming out of your eyes, or maybe you get the cold chills that sometimes accompany hearing a good song at a certain time? You can, maybe (I haven't really looked), find some literature out there that studies these phenomenon that will break it down into release of neurotransmitters in specific portions of your brain or up/downregulation or some sort of protein or what-have-you, however we will never really know how all that 'chemical/electrical soup' translates into our 'feel good' perceptions as the human experience/consciousness is said to be an emergent property.