In short, your brain likes for things to make sense. What's more, your brain obsessively needs to make sense of itself to itself. That is, every action you do and every thought you think must be rational and coherent to your brain.
That is what it wants, but that is not often what happens.
For instance, there was a study in which people were locked in a room, deprived of any sort of entertainment or stimulation (no books, no phones, no communication, no windows, no food, no water, nothing to look at, nothing to listen to, etc.) *except* there was a button on the table. If they pressed the button, they were shocked.
They weren't told *not* to press it. They weren't told to press it. They weren't told that it would shock them, though most people figured that out in a few minutes. No, the interesting thing was that people would *willingly shock themselves again* to alleviate the boredom.
And then, when the psychologists asked them about repeatedly shocking themselves, **the people made up bullshit.**
Or, take the case of split-brain people. The two halves of their brain can no longer talk to each other, and one side, the right side, cannot talk at all. However, the right side can still read, so if you show only its eye a sign that says "pick up the ball", then ask the person why they picked it up, you'll often get answers like, "Because I wanted to" or "I like playing with balls".
People occasionally perform actions that are stupid, self-destructive, or inexplicable to themselves, and their minds can't handle that. Instead, it imagines a justification for the behavior that explains it in a way that is consistent with their sense of self.