Pain severity and sleep are associated with cognitive performance in patients with fibromyalgia. This study examined whether sleep mediates the relationships of pain severity with psychomotor vigilance and attention in patients with fibromyalgia by analysing 80 patients with fibromyalgia. Cognitive performance, pain severity and sleep parameters were determined using the Psychomotor Vigilance Task, Brief Pain Inventory-Short Form and sleep diaries of seven consecutive nights, respectively. The patients' demographic data were analysed for potential confounding factors. After adjustment for these confounders, a series of regression analyses was performed to examine the mediating role of sleep. The results indicated that higher pain severity was strongly associated with poorer sustained attention and lower sleep quality, the total effects of pain severity on psychomotor vigilance and attention were significant (c path: β = 0.23, p = 0.04), and pain severity was a significant sleep quality predictor (a path: β = -0.33, p < 0.01). When sleep quality was entered into the regression model (a × b path), the effects of pain severity on psychomotor vigilance and attention became non-significant (c' path: β = 0.15, p = 0.20) after adjustment of age, indicating a complete mediating effect of sleep quality in the pain severity-cognitive performance relationship. In conclusion, sleep quality mediates the pain severity-cognitive performance relationship: pain affects sleep quality, which in turn impairs sustained attention. Our findings provide further insight into the processes underlying the relationship between pain and poor cognitive function. Improved sleep quality may offset the detrimental effects of pain on sustained attention. © 2019 European Sleep Research Society.