We get to hear David's last words… but we don't get to see him die. That won't happen until the beginning of 1 Kings. It's an out-of-order way of doing things, but so what? We guess the editors just realized there were some good things they needed to get in quick, and who cares if they interrupt the story in the process?
The end of 2 Samuel has a lot of grab bag stuff that seems sort of tacked on:
- David's thanksgiving prayer to God,
- his last words,
- a roster of his top warriors,
- and finally the Census story, where we see David provoke God's wrath against Israel.
It's an odd way of doing things, and it doesn't really provide "closure" for the reader, though the census tale is a self-contained story with a satisfying conclusion. But that's because you're supposed to keep reading. You'll have to wait for 1 Kings for the resolution of David's story.