Mescaline is 3,4,5-trimethoxy-phenethylamine, MDMA is 3,4-methylenedioxy-n-methyl-amphetamine
Mescaline is a phenethylamine, MDMA is an alpha-methyl-phenethylamine (amphetamine)
First, let's describe the difference between a phenethylamine and an amphetamine. A phenethylamine is a benzene ring with a 2 carbon ethylamine side chain ( ph-ch2-ch2-NH2 ), the CH2 groups of the ethylamine side chain are labeled as the alpha and beta carbons, the carbon connected to the amine nitrogen being the alpha carbon. Now, "amphetamine" is short for "alpha-methyl-phenethylamine", so in chemical structure, the difference between an amphetamine and a phenethylamine is a methyl (CH3) grouping attached to the alpha carbon of the ethylamine side chain which is connected to position 1 of the benzene ring.
Structurally, MDMA and mescaline are both phenethylamines, and share many identical features. Pharmacologically these compounds are fairly unrelated.
When you take amphetamine, which pharmacologically is primarily a dopamine/adrenoceptor related compound, and place methoxy groups at 2 and 5, or a methylenedioxy grouping at 3 and 4 this dopamine and adrenoceptor pharmacology shifts, now involving serotonin and serotonin receptors.
MDMA and mescaline are both phenethylamines, and as far as chemical structure is concerned they are closely related, however, mescaline is a true psychedelic, while MDMA is an entactogen/empathogen. So chemically they are related, while pharmocologically and phenomenological they are hardly related at all.
(There are many drugs with psychedelic features which are not truly psychedelic, like MDMA, THC and cannabinoids, PCP, ketamine, tropane alkaloid deliriants such as those found in datura, and so on, when I say "psychedelic" I am not referring to these compounds, I am speaking of the compounds which are predominantly 5HT2a/c receptor agonists )