"His majesty, King Juan Carlos, has just communicated to me his will to give up the throne," Rajoy said, adding, "I'm convinced this is the best moment for change."
Once popular Juan Carlos, 76, who helped smooth Spain's transition to democracy in the 1970s after the Francisco Franco dictatorship, has lost public support in recent years due to corruption scandals and gaffes.
His daughter, Princess Cristina, and her husband, Inaki Urdangarin, are under investigation in a corruption case.
The king, whose health is failing and has had a number of hip operations in recent years, is stepping down for personal reasons, Rajoy said.