Abstract Background The Mediterranean diet (MedDiet) has demonstrated efficacy in preventing age-related cognitive decline and modulating plasma concentrations of endocannabinoids (eCBs) and N- acylethanolamines (NAEs, or eCB-like compounds), which are lipid mediators involved in multiple neurological disorders and metabolic processes. Hypothesizing that eCBs and NAEs will be biomarkers of a MedDiet intervention and will be related to the cognitive response, we investigated this relationship according to sex and apolipoprotein E ( APOE) genotype, which may affect eCBs and cognitive performance. Methods This was a prospective cohort study of 102 participants (53.9% women, 18.8% APOE -ɛ4 carriers, aged 65.6 ± 4.5 years) from the PREDIMED-Plus- Cognition substudy, who were recruited at the Hospital del Mar Research Institute (Barcelona). All of them presented metabolic syndrome plus overweight/obesity (inclusion criteria of the PREDIMED-Plus) and normal cognitive performance at baseline (inclusion criteria of this substudy). A comprehensive battery of neuropsychological tests was administered at baseline and after 1 and 3 years. Plasma concentrations of eCBs and NAEs, including 2-arachidonoylglycerol (2-AG), anandamide (AEA), oleoylethanolamide (OEA), palmitoylethanolamide (PEA), and N- docosahexaenoylethanolamine (DHEA), were also monitored. Baseline cognition, cognitive changes, and the association between eCBs/NAEs and cognition were evaluated according to gender (crude models), sex (adjusted models), and APOE genotype. Results At baseline, men had better executive function and global cognition than women (the effect size of gender differences was − 0.49, p = 0.015; and − 0.42, p = 0.036); however, these differences became nonsignificant in models of sex differences. After 3 years of MedDiet intervention, participants exhibited modest improvements in memory and global cognition. However, greater memory changes were observed in men than in women (Cohen’s d of 0.40 vs. 0.25; p = 0.017). In men and APOE -ε4 carriers, 2-AG concentrations were inversely associated with baseline cognition and cognitive changes, while in women, cognitive changes were positively linked to changes in DHEA and the DHEA/AEA ratio. In men, changes in the OEA/AEA and OEA/PEA ratios were positively associated with cognitive changes. Conclusions The MedDiet improved participants’ cognitive performance but the effect size was small and negatively influenced by female sex. Changes in 2-AG, DHEA, the OEA/AEA, the OEA/PEA and the DHEA/AEA ratios were associated with cognitive changes in a sex- and APOE -dependent fashion. These results support the modulation of the endocannabinoid system as a potential therapeutic approach to prevent cognitive decline in at-risk populations. Trial registration ISRCTN89898870.