The Millennium Puzzle is one of the seven Millennium Items. It houses the spirit of the Ancient Pharaoh, also known as Dark Yuugi, who sacrificed himself 3000 years ago (5000 years in the dub) to prevent the destruction of Egypt.
The anime and the manga differ on where the Millennium Puzzle came from, and how exactly it came into Yuugi Mutou's possession. The anime gives the clearest explanation, stating that in 1960, Yuugi's grandfather, Sugoroku Mutou hired two Egyptian men to take him into a legendary tomb filled with traps and games. The two men only planned to let Sugoroku live as long as he solved all the puzzles. However, both of them died, and one of them shot Sugoroku. He would have died as well, though he came extremely close to finding the Millennium Puzzle, disassembled and placed in a golden box decorated with hieroglyphs and the Eye of Horus. Before a severely-wounded Sugoroku fell to his death, he saw the spirit of the ancient Pharaoh, who saved his life and allowed Sugoroku to leave the temple with the puzzle pieces...
The manga doesn't explicitly say where the puzzle came from, although Sugoroku Mutou does tell his grandson that it was found by a team of British archaeologists at the beginning of the 20th century (1900-1999). After each of the archaeologists died mysterious deaths, the puzzle somehow came into game-aficionado Sugoroku Mutou's hands. Perhaps to scare Yuugi and his friends, Sugoroku also told them that the last surviving archaeologists, prior to his death, said these words: "The Shadow Games..." (see Notes)
In the anime, it is clearly stated that Sugoroku gave the puzzle to his grandson eight years from the start of the story (which would be when Yuugi was seven or eight years old). It wasn't until he was in high school and brought the puzzle to school that he came close to solving it at last.
In the anime, it is not stated how long it took Yuugi to solve the puzzle. In Yu-Gi-Oh! The Movie (also known as "Yu-Gi-Oh Duel Monsters: Pyramid of Light") Yuugi solves the puzzle at approximately the same time as a team of archaeologists unearth the tomb of Anubis. He does not receive the final center puzzle piece from Jounouchi, as he did in the manga, but instead has it in his puzzle box the entire time.
Note: The concept of "The Shadow Games" and "The Shadow Realm" exist only in the original English dub of the anime. The English dub was remade and re-released unedited and uncut on DVD starting in October 2004, and almost exactly follows the original Japanese script, though it retains the English dub name changes.