There are two likely things that can cause this sort of thing. The VSS (vehicle speed sensor) and the MLPS (manual lever position sensor).
When the VSS acts up, you will also loose the speedometer.
The MLPS bolts to the trans where the shift linkage attaches. In times past, it was called the neutral safety switch. Today it also tells the computer which gear has been selected. If the readings get flakey, it can cause all sorts of unusual transmission shifting problems. One of the reasons they sometimes get flakey is that the internal lubricant grease gets old and starts to run and creates additional electrical signal paths.
It is hard to diagnose a car without driving it, and there are other things that could explain your problem, but these are worth checking out.