It's not the cordless drill - it's the keyless chuck on the cordless drill. Those will self-loosen (due to the inertia of the adjusting sleeve) if the drill is stopping suddenly. Or if it's started suddenly in reverse.
So if you are driving screws at top speed and letting the torque overload stop the driving, or otherwise stopping suddenly, they are likely to loosen. Mine will unscrew itself and release the bit if I have it running at speed and let off the trigger suddenly, because it has a spindle-lock when the trigger is off, so it stops quite abruptly. The outer sleeve you grasp to tighten it wants to keep spinning.
Simply slowing down as you get close to finishing each screw will reduce that problem vastly.
Not simply, using a keyed chuck would do it, but that's going to be you replacing the chuck on your cordless drill, and is a hassle and expense.
Or the serious off-grid and stealthy-quiet solution is to use a bit-brace to drive your screws. Back when NiMh was the top technology for your rechargable drill, after I used up my two batteries I'd grab this out of my bucket and keep going, blowing the minds of folks also waiting for their batteries to slowly charge. That's been less of an issue since Lithium-ion rechargables with fast chargers showed up.

As mentioned in comments, the brace in the picture above has a standard brace chuck which is holding the hex bit in jaws not really designed for it (though it works fine in my experience.) An adapter is available, though I have not purchased one.

Image sourced from Lee Valley Tools no endorsement implied.