The upgrade reflects the government's steps to "address the weaknesses in the banking sector" and an "increasingly balanced growth profile," despite remaining "institutional weaknesses," the agency said in a statement.
The agency moved the debt rating up a notch to the upper-medium investment grade Baa1 and said the rating outlook remained stable. The decision follows similar moves by Fitch and S&P Global earlier this year. Spain was downgraded in 2012 by all major ratings agencies amid fears Madrid could require a bailout due to the weakness of its banks.
Spanish Prime Minister Mariano Rajoy welcomed the fourth debt upgrade, saying that thanks to the governments reforms and "the efforts of all Spaniards, confidence in Spain has increased."
"We cannot stop now," Rajoy said on Twitter.
According to Moody's, the political crisis in Catalonia province, which has sought to secede from Spain, has "abated somewhat" and has so far not harmed Spain's fiscal or economic performance.
"While we expect political tensions between the central government and pro-independence forces in Catalunya to remain elevated for the foreseeable future, our baseline is still that Catalunya will remain part of Spain,"
Moody's said. Catalonia accounts for 20 percent of Spanish GDP.
The Spanish government's difficulties in setting a budget, which were due in part to the crisis with Catalonia, should not cause a "negative policy shift" in the coming years, the statement said.
High economic growth of three percent in recent years should moderate towards 1.5 to two percent in the medium term, it said.
Spanish banks' stock of non-performing loans has been falling steadily since late 2013, hitting 7.4 percent as of the end of 2017, down from 12.9 percent four years earlier.