The bug
If you lock your difficulty in then go into a different dimension the lock is undone. If you lock it in that dimension and go back to the original one. It will be undone again.
Code analysis
Code analysis by @unknown can be found in this comment.
Related issues
is duplicated by
Comments


Does switching dimensions, logging out of the world, then logging back in fix the visual issue?

Oh yes it is just a visual glitch. Thank you for helping!

Still an issue in 08a and 10c.

Still in 14w11b

Confirmed for 14w17a

I can confirm this is present in 1.8.1. It appears to be a gui-related issue. You can cycle the button to peaceful, but hostile mobs will not despawn.

I'm having the same issue in 1.8.7. The difficulty is unlocked and reverted to Easy upon moving between dimensions.
Ubuntu 14.04 64-bit system with Intel HD integrated graphics card using Oracle 8 Java Runtime Environment

Please link to this comment in the description
The following is based on a decompiled version of Minecraft 1.9 using MCP 9.24 beta.
The reason for this is the same as for MC-679. When the player enters a different dimension a new WorldClient
object is created an most of the data is copied from the previous WorldClient
object. Whether or not the difficulty is locked is only send when the player joins a server (or loads a world). When he changes the dimension the method net.minecraft.client.network.NetHandlerPlayClient.handleRespawn(SPacketRespawn)
does not set whether or not the difficulty is locked for the new WorldClient
.

Is it a visual glitch tho? I've been able to cycle my difficulty (Locked at hard from world gen) and it still gives the whole blurb about locking the difficulty when I click the button.
I haven't actually played on any other difficulty to see if there was a difference and I haven't cycled to peaceful (In fear some of my named hostile mobs would despawn)
Also, confirmed 1.9.4

Confirmed for 1.13.1.

update 1.12.2 still
Also, only the button is reset; the difficulty is still locked.