Mine only does it in 5th gear under moderate-heavy load....its only good for gentle cruising. I believe its worn out synchros?
My parents forester did this in 5th gear too and it was a bad synchro but you basically have to get the transmission rebuilt to fix the problem. That or just not drive in first gear? That's just my experience with that.