Pedro Sanchez won the no-confidence motion with 180 votes in favor, 169 against and one abstention, according to the Associated Press.
To prevent a power vacuum after a no-confidence motion that saw Mariano Rajoy ousted, Spanish law makes the motion's author - in this case, Sanchez - the country's new leader as soon as the king swears him in.
But it is unclear how long his administration, with only 84 Socialist deputies in the 350-member legislative assembly, can last. Sanchez and his party are staunch supporters of the European Union and the euro currency shared by 19 European Union nations.
Lawmakers stood and cheered in parliament as the untested 46-year-old - a pro-European lawmaker who has never been in government - became the country's seventh head of government since its return to democracy in the late 1970s.
Sanchez said on Thursday he would stick to the budget put forward by Rajoy and approved by parliament last month if voted into power.
The incoming prime minister says his priorities will be social issues - including more measures to help young people and the elderly.
Still, Sanchez will face a tough time catering to demands from the small parties whose votes he captured in the no-confidence motion, among them Catalan separatists.
Trump Suggests Separate Deals With Canada, Mexico To Replace NAFTA
The EU earlier threatened to counter punch by targeting U.S. products, including Kentucky bourbon, blue jeans and motorcycles . Mexico said it will impose wide-ranging "equivalent " measures, including on American steel and farm products.
It is the first time a serving leader in Spain has been removed by the parliament in Madrid in four decades of democracy. Many Spanish voters, exasperated by corruption scandals involving the traditional centre-right PP and centre-left Socialist parties, have abandoned them for newcomers like the left-wing Podemos (We Can) and centrist Ciudadanos (Citizens), as well as regional parties.
That ruling spelled the end of Rajoy's rule, and Sanchez was keen to portray himself as a fresh start.
Madrid fired Catalonia's government and took control of the running of the region after an illegal and unsuccessful declaration of independence by the Catalan parliament in October.
Last year, he survived a motion of no confidence led by Podemos, a far-left party.
In his first cabinet list presented on May 19, Catalan leader Quim Torra included candidates either in exile as a effect of the independence bid or under investigation for their role in it.
The members of Catalonia's former government are to be tried on charges including rebellion.
The Socialist leader must still name his cabinet and it is only when their names are published in an official government journal in the coming days that he will fully assume his functions.
Torra is a fervent Catalan nationalist and was hand picked by former Catalan leader Carles Puigdemont to succeed him.