I just replicated and fixed exactly this behaviour (with 2016 F54).
Having read OP, I tested how my car behaved, using the spare key.
Purely by chance the spare key's battery was on the way out - It still worked well enough for its buttons to lock/unlock etc, but not well enough for any of the comfort access features to work (so not the little buttons on handles).
In this state things worked exactly as for OP - key got locked in.
Replaced the keyfob battery and things work as advertised: On closing rear door with key inside the car beeps and thows open the rear door. (comfort access stuff also worked again).
Hope this is applicable to OP's car.