The motion submitted by the opposition Socialists asks the conservative government to recognise Palestine in coordination with any similar move by the European Union.
It was adopted nearly unanimously in the lower house of parliament, with 319 in favour, two against and one abstention.
The vote came after Tuesday's attack at a Jerusalem synagogue that saw two Palestinians armed with a gun and meat cleavers kill four people before being shot dead. The vote had been planned beforehand.
The text asks the Spanish government to "recognise Palestine as a state, subject to international law," while adding that the "only solution to the conflict is the co-existence of two states, Israel and Palestine," reached through negotiation.
Signup to our newsletter and follow us on Facebook and Twitter!
"It is not binding, it does not set a timeline for the recognition, it gives the government the margin to proceed with the recognition when it feels it will be opportune," Spanish Foreign Minister Jose Manuel Garcia-Margallo said in Brussels on Monday.
"If we want to be effective this recognition must be done in coordination with the European Union," he added.
The motion follows moves in other European countries intended to increase pressure for a two-state solution to the Israeli-Palestinian conflict.
Britain and Ireland approved similar non-binding motions last month that call on their governments to recognise Palestine. Neither government has heeded that call.
French lawmakers will vote on November 28 on a proposal by the ruling Socialist party urging the government to recognise Palestine as a state.
Sweden's new left-leaning government went a step further and officially recognised a Palestinian state on October 30, prompting a strong protest from Israel, which swiftly withdrew its ambassador from Stockholm.