Suppose I hear a simple melodic phrase and then can sing it back. That's rather mysterious, because many people can do that. We aren't consciously producing a certain series of notes, but just able to mimic what we've heard. Then someone who can play a melody on the piano for a song they already know. I can do it somewhat, but not perfectly in all cases. Now that unconscious ability to mimic, we can do with an object exterior to the body. The process is still unconscious, in some ways, because people who can do this naturally aren't thinking, oh, "that's a minor third up." They just know how to exteriorize that pitch matching with a keyboard. Of course, conscious thoughts can occur too; I'm just saying that the process is not overall a conscious one.
Then, improvising, I can hear a phrase in my head and then play it. I'm not sure how I do it. As I like to say, the thing about it that surprises me is not that I can write great songs, or improvise very well, but that I can do it all.