The story
Combination of rack (“medieval torture method”), and brain, thus meaning “to cause the mind extreme discomfort”. Variants include rack one's wit (1583) and rack one's memory, as well as cudgel one's brain / cudgel one's brains.
Etymology from Wiktionary, CC BY-SA 4.0.