Teaching, research and writing. I hate to be that bland, but that's what it amounts to for most of us. When you teach a particular subject year after year, it locks the details in. When you do archival research and write at least some of that detail into publications or even just notes, it locks in more of it. To some extent you are the beneficiary also of fellow historians and their eye for detail and information--e.g., in some fields, enough people notice and repeat some of the same illustrative details and they become part of our collective disciplinary memory.
As u/swarthmoreburke mentioned, the training, research, and writing (and sometimes teaching, depending on the person) that we all do on a regular basis helps us remember details that other people wouldn't think necessary to remember. Practice and repetition are great teachers.....and I guarantee you that if you've spent 50+ hours researching a topic and then writing a 10-20 page paper on it, you'll remember at least some of it. Now multiply that by about 10 times as many papers, and you start getting the idea.
Additionally, for myself in particular (and I suspect for many of those who post on this sub and work in various history-related fields), remembering weird facts and fun stories comes naturally. This is also helped by a genuine passion for history/history-related fields. It's really no different than a passionate nerd who can real off all of the starter Pokemon and their evolutions or your average TMZ-obsessed teenager that knows a seemingly endless amount of inane celebrity gossip. I often joke that around 80% of the information I carry around in my brain and occasionally pull out for interesting small talk at parties is functionally useless, but at least I'd be killer at Jeopardy.
Think about it this way: have you ever accidentally stumbled into Internet Discourse about a piece of media (book, tv show, movie, etc) where people are debating some aspect of said piece of media? And the people involved are attempting to use their understanding of the media they consumed to debate intelligently about the issue involved? And in order to do that, they have to have a certain level of comprehension and information retention about what they just read/watched so they can use plot points/events/character interactions to prove their point?
......what historians do is like that, except for real life. Same principles apply: we read, research, consume, think critically about what we've read, and then write out our thoughts about the topic. Thinking critically about something you're reading/watching will always help you remember certain aspects of it more easily, especially if you know you're going to have to formulate and articulate your own 'summary' and thoughts on what you read.