CAN you? Certainly. It's not like the Fiction Police will arrest you for writing such a story. The question is, can you do it well enough to make the story interesting and believable? The trick here is to make the "imaginary" character concrete enough that he is believable, but not so distinct from the other personality that when the reveal comes, readers find it disconcerting.
I read once -- sorry, I don't remember the source so can't give proper credit -- that in a well-written story, the ending follows logically from what came before, so that when the conclusion comes the reader feels that it makes perfect sense, but it is also a complete surprise. That's hard to achieve as those are competing goals, but that should be the ideal. I'm sure we've all read stories where some reveal just comes out of nowhere, where we are just suddenly told that aliens have landed and there was absolutely no clue in the story before that that this was anything about aliens. (Or whatever.) And likewise I'm sure we've all read stories where the ending is so obvious that when it finally comes you're like, "well duh, that was obvious from page 3".