Monster Room

Dungeons are naturally generated structures that appear in the Overworld.

Generation
Dungeons generate naturally in the Overworld, at any level. For each chunk, 8 attempts are made to spawn a dungeon; it's possible but unlikely for multiple attempts to succeed.

For each attempt, a location and size (an open area of 5×5×3, 5×7×3, or 7×7×5) are chosen. The attempt succeeds if the following conditions are met:
 * The floor area (including under the walls) of the potential dungeon must be entirely solid.
 * The ceiling area (including over the walls) of the potential dungeon must be entirely solid.
 * The walls of the potential dungeon must have 2–5 openings (2-high air blocks) at floor level.

If the location passes, air and cobblestone are placed, then 3 attempts are made to spawn each of 2 chests. To spawn a chest, the chosen block must be empty and must have a solid block on exactly one of its four sides. The spawner is placed at the center of the dungeon.

Structure
Dungeons are small rooms made of cobblestone and moss stone and contain a monster spawner and up to 2 chests. Finding a dungeon without a chest is very unlikely, but possible. Occasionally, a dungeon will spawn with its chests in such a way as to create a double chest. Dungeons generate with either a zombie (50% chance), skeleton (25% chance), or spider (25% chance) spawner. The spawner is always in the center of the dungeon room, with chests located around the walls of the room (large chests can connect with the short side against the wall) Each block of the floor has a 25% chance of being cobblestone, and a 75% chance of being moss stone. Dungeons will still generate if the 'generate structures' option is toggled to off.

Loot
Each dungeon chest block contains up to 8 item stacks, with the following distribution:

In Pocket Edition, cocoa beans and ink sacs can be found in chests.

This information was obtained from Minecraft's code. The generation algorithm places one of these items in a random slot in the chest eight times, possibly overwriting items already placed.

Video
''Note: This video was created before the release of. Since then, mossy cobblestone can be found in Jungle Temples (12w22a) and Mega Taiga biomes (13w36a).'' ''Note 2: Cocoa beans no longer generate in dungeon chests. Name tags and horse armor were added to chests later in 1.6.''

Trivia

 * Dungeons are always connected to a cavern or other structure, although it's possible for the dungeon itself to overwrite most of a very small cave.
 * It is possible, though rare, to find connected dungeons with multiple spawners and extra chests. It is also possible for a dungeon to spawn over another, replacing the chests and spawner of the first.
 * It is possible, though almost impossibly rare for a dungeon to be generated at bedrock level, thereby deleting the bedrock. In which case digging through the moss stone can lead into the Void.
 * Double-chest dungeons as shown below may have chests that form a perpendicular barrier between the wall and spawner (an exception to this would be in 7x7 dungeons, in which the chest cannot reach the spawner):