This sounds like a problem in the gear shift linkage somewhere, the shifter mechanism or cable seizing when heated due to the expansion of metal, or the lubrication applied to those components has lost its mojo.
First, if you can get under the car I'd check to make sure the shifter cable is not kinked, restricted in some way, or coming in direct contact with the engine. Touching the engine will cause the cable to heat and that could make it seize, routing it away and making sure it isn't bent somehow could resolve the issue.
Lubrication may also help if you can get at the right areas, a spray of lithium grease and exercising the mechanism could fix it right up. You could try lifting up the fabric gaiter/boot that covers the shifter mechanism and getting some spray lithium in there (WD40 may help initially but it isn't a long-term solution). If you can get access then a spray at both ends of the cable and the transmission lever connections would be a good idea too.
If that works, happy days, if it doesn't then you should replace the whole shifter/cable assembly, they seem pretty cheap and you could sink a lot of time trying to troubleshoot it.
There's the much lower chance that it's the automatic transmission that's at fault, in which case you could try replacing the ATF, but most likely you'd need it professionally serviced.