Mr Jansa’s Slovenian Democratic party took about 24% of the vote, compared to 34.5% for the Freedom Movement party led by former businessman Robert Golob.
The poll was marked by allegations that Mr Jansa has undermined the rule of law in the former Yugoslav state.
Mr Golob said his win will enable him to lead his country “back to freedom”.
An outspoken supporter of former US President Donald Trump, Mr Jansa was seeking a fourth term a prime minister and stood on a platform of “stability”.
But his critics allege that the 63-year-old, who came to prominence as a post-communist reformer, has spent several years eroding democratic standards and restricting press freedom.
He engaged in lengthy rows with the EU over his moves to cut funding for the national news agency and has sought to delay the appointment of prosecutors to the bloc’s new anti-corruption body.
Mr Golob is a political newcomer who entered politics in January after being removed as chairman of his energy investment firm.
He had billed the election as a “referendum on democracy”.
The 55-year-old celebrated his victory in isolation from his home after contracting Covid-19 and told jubilant supporters via livestream that “people want changes and have expressed their confidence in us as the only ones who can bring those changes”.
He is expected to form a governing coalition of other left-wing parties.
Turnout for the vote was significantly higher than expected, with some 70% of the country’s 1.7 million residents going to the polls – an increase of 18% on 2018’s poll. A much tighter race had also been predicted.