I often don't know where the words come from... that seems like a startling admission for a writer to make, but it's true. Although I'm a plotter rather than a pantser when it comes to my writing, I sometimes end up surprised by what comes out of my brain through my fingers.
A week ago, I wrote a section of the novel I'm working on, a mystery, in which one of the protagonists related a story about his maternal grandfather and an odd encounter with a polar bear in Iceland to a pair of tertiary antagonists. I enjoyed the story as it came out and found its home in my story (a meta-story?) but had no idea how and why it would fit.
Often, when I'm writing a first draft, I don't worry about the how and why, or even if, it fits. I've got a general framework of the story in notes and on my working storyboard, so I feel free to let my mind wander a bit within that framework and am usually pleased with the results; when I'm not, when it doesn't work out, I can always chop the offending, or oddly fitting, piece.
I wrote for four hours, minus breaks for coffee input and outflow, and then the words slowly trickled to a stop. The encounter, which is critical for a lot of the closing of the novel, had come together better than I had hoped, and after setting up the next chapter, my brain signaled my fingers that we were done for the day.