Updated:

Java: show your death coordinates

/data get entity @s LastDeathLocation

The chat shows something like this:

{dimension: "minecraft:overworld", pos: [I; 120, 64, -30]}

dimension is where you died and the three numbers in pos are X, Y and Z. This works on Java 1.19 and later, with cheats on or operator rights on a server. If you have never died in the world, you get a "found no elements" error.

Teleport back

/tp @s 120 64 -30

Use the three numbers you got. If you died in the Nether or the End, name the dimension too:

/execute in minecraft:the_nether run tp @s 120 64 -30
/execute in minecraft:the_end run tp @s 120 64 -30

If lava or the void killed you, the same spot will kill you again. Add a few blocks to Y, or take fire resistance first with /effect give @s minecraft:fire_resistance 60 0. More in the tp guide and teleport to the Nether or End.

Another player's death spot

/data get entity Steve LastDeathLocation
/execute in minecraft:overworld run tp Steve 120 64 -30

Swap in any online player's name. Server admins can use this to send a player back to their items.

Recovery compass (Java and Bedrock)

/give @s minecraft:recovery_compass

The needle points to where you last died. On Bedrock use /give @s recovery_compass. It spins if you are in a different dimension or have not died yet. In survival, craft it from a compass and 8 echo shards, which come from ancient city chests.

Bedrock: no command for death coordinates

Bedrock cannot print where you died. Combine these instead:

How long your items last

Dropped items vanish after 5 minutes while the area is loaded. The timer pauses while the chunk is unloaded, which is why a quick teleport back helps.

Keep your items next time

/gamerule keep_inventory true

You keep your items and experience when you die. This is the Java 1.21.11+ name; use keepInventory on 1.21.10 and older and keepinventory on Bedrock. See the keep inventory guide.

When it does not work

Check your command

The Command Error Doctor checks common syntax mistakes in your browser.