Ah, I remember this phenomenon – or at least I think what I'm remembering is what you're describing – from learning to improvise.
What I recall is that the bad rhythm moments happened when I was trying to just throw a whole bunch of notes out there really fast. Maybe it was because I was nervous, and I was just throwing a bunch of pitches at the wall to see what stuck, or maybe it was because I had decided I wanted to do something that sounded busy and flashy.
The correct solution for me rested on the venerable principle of "SLOW THE @#%! DOWN". Simply not trying to play so many notes per unit of time helped greatly. I calmed down and was able to focus enough of my attention on the feeling of the rhythm to join with it. And that's crucial: instead of flailing at my instrument to make a bunch of pitches, I needed to think about what I wanted to do rhythmically, and make some deliberate artistic decisions about how I wanted to relate to the rhythm.
And I learned the trick to putting in flashy fast stuff is that it's got to be conceived of -- and mastered through practice as -- licks/figures/gorgias/whatever you call them: little chunks of sparkly stuff that you have trained into your hands/mouth as a spinal reflex, and can fire off at any moment, and, crucially, you think of not as a series of notes, but as a single thing. If you can think "Mordent here!" instead of "really fast I'll go up then down!", you've radically reduced your cognitive load -- by 2/3rd actually.