The outcome delivered a blow to Law and Justice, which has governed Poland since late 2015, while providing a psychological boost to opposition parties, which had put feuding aside and united behind a joint candidate.
Opposition candidate Konrad Fijołek won 56.51 percent of the vote, while the contender backed by PiS, Ewa Leniart, garnered 23.62 percent, according to official results.
Marcin Warchoł, who hails from Solidarna Polska, a junior partner in the conservative coalition government, won 10.72 percent.
Solidarna Polska put up its own candidate in what was seen as a sign of divisions within the ruling coalition.
Meanwhile, right-winger Grzegorz Braun garnered 9.15 percent.