Put an empty magazine in the gun. Then rack the slide back sharply and let it go. If it locks open you don't have a mechanical issue with the gun or the mags or any of that. Try this a few times to make sure the result is the same. If it is not locking open, save yourself some grief and take it to a good gunsmith.
I strongly suspect that your gun will lock open every single time on this function check though. In my opinion, a lot of very questionable advice is being spouted here about replaceing this or that part. If the parts are passing the type of function check described above then there is no need to replace them.
While we don't have all the info, the main culprits here are the ammo (yes, the ammo) you are using and the way you are holding the gun (and maybe the recoil spring, see below). All experienced shooters know that the way you hold any auto pistol has an effect on the cycling of the pistol and the ejection of spent cartridges. My girlfriend runs into this problem a fair bit because she is small and she tends to limp risk her gun. When she shoots her Beretta the spent casings usually look like they are just barely making it out of the gun and start heading for the ground about two feet from her. This was shooting 115gr. factory 9mm. She was getting a few different kinds of cycling failures ("stovepipes" and what I call the "last one not quite out, next one not quite in" failure) and - important for our purposes here - the gun was not locking back on the last shot.
Yesterday one of the range officers at our range was shooting her gun (because she was getting failures and wanted his advice) and when he shot her gun the spent casings were flying out of there and clearing about 3 or 4 times the distance. No failures of any kind. No problem locking back on the last shot. I tried the gun too. Same results.
Using 147gr. she doesn't get these problems. The extra "umpf" of the round just powers through the problem. The same basic idea applies with the other calibres. Most factory .45 Auto is 230gr., but not all of it is. Even just trying a few different brands of ammo and you will likely get some different results.
Now, I don't really want to tell some dude that he is probably limp wristing his $2,000 Mark 23, so I said get some hotter ammo. I think that will clear up the problems faster than improving your shooting style will (and your shooting will improve with time too).
I think a really stiff recoil spring may have some roll to play in these issues as well, but I would tend to discount that with an HK. The recoil reduction system is not something you want to mess around with too much unless you really know what you are doing. I have not had this problem with my USP, but I readily admit there is a lot of distance between a USP and a Mark 23. I know that the Steyr M9-A1 I have has a very very stiff recoil spring and the "fail to lock open" problem is endemic to that gun with 115gr. ammo (as is the very short ejected case trajectory). The reason why some of these guns have such stiff recoil springs is because they are designed to shoot hot (military) ammo... so yes, ammo does matter.