The problem might be with the Idle Air Control valve. This is what controls the engine idle speed anytime the accelerator is released.
This valve goes to different positions depending on the load on the engine at idle. So for example, if the AC is turned on at idle the valve goes to a position that prevents the engine from stalling from the load the AC imposses on the engine. Or if the steering wheel is turned when idling, the valve goes to a position that prevents the engine from stalling from the load impossed on the engine from the power steering pump.
So in your case, the valve may not be going to the proper position to prevent the engine from stalling when the vehicle is in gear, and the load from the drivetrain is impossed on the engine. But when put into neutral, the load on the engine from the drivetrain is removed and the engine doesn’t stall.