def electrical .
Look down at the brake pedal for the switch that turns on the brake lights. That also tells the interlock solenoid to release the shift lever. Look for bent or broken bracket for that switch, maybe unplugged wiring, or something like that. Or maybe the switch has just gone bad...
Get in the car, turn the key on, and push the brade pedal just slightly down. You should hear a sort of clicking sound comming from near the shift lever. then again when you release it. (shift release solenoid). Try it when it does work right so you will know what it sounds like.(it's not loud) Also next time it happens check to see if the brake lights work when you push the brake. (brake light switch).