When Melinda found out that Matthew was a warlock who copied her powers, he revealed to the townspeople that she was a witch. In revenge, Melinda created a curse, the "Warlock Curse" and cursed Matthew into the locket when he came to visit her in the town's jail. Not long after, Melinda was burned at the stake. According to a book that Piper read on Melinda and Matthew, the legend says that the warlock must never be freed or he will destroy the Warren line.
Before Melinda's death
Before her death, Melinda enacted her revenge on Matthew Tate by cursing him to spend eternity trapped within a locket he had given to her as a gift, but not before wiping out Matthew's copies of her powers. Only Melinda herself or a witch of blood kin could set Matthew free.
300 years later
Over 300 years after her death, in 1999, Melinda's great-great-great-great-granddaughter, Prue Halliwell, was given the cursed locket while working as an appraiser at Buckland Auction House. Not knowing of the curse, Prue opened the locket and set Matthew Tate free. What Prue didn't know was that her boss, Rex Buckland, was using Matthew to steal her powers.
Realizing that she had to have been a descendant of Melinda, Matthew tried to kill Prue, but she fought him off with her telekinesis, thus inadvertently enabling him to copy that power. He now had a new M.O: copying the powers of The Charmed Ones and then killing them, effectively destroying the Warren line and enacting the ultimate revenge against Melinda for entrapping him for over 300 years.
Bringing Back Melinda Warren
Fearful for their lives, the Halliwell sisters cast a spell to bring Melinda back and into their time. Melinda is gratified that the three sisters have fulfilled her prophecy of 300 years earlier, and proposes to curse Matthew back into the locket, presumably for good.
Before they could finish the potion with which they can curse Matthew back inside of the locket, Matthew himself arrived at the manor having copied Phoebe's power of premonition. Now all Matthew needed was a copy of Piper's powers before he would be powerful enough to kill the sisters and then Melinda as well.
Fortunately, the collective workings of the sisters and Melinda were enough to foil Matthew. Prue delivered the last ingredient to the potion, the feather of a spotted owl, and Melinda uses the completed potion and its adjoining spell to once again trap Matthew within the locket for all of eternity.
After the Battle
After the battle Melinda returns to the afterlife with the locket to keep Matthew trapped forever, but before the Halliwell sisters released her spirit, Melinda receives a glimpse of a distant future with her power of premonition. She sees many more generations of "her daughters" through Phoebe, proving the Warren line will live on for years to come.