That's what you get with coalition governments, especially if the only feasible coalition excludes the largest parties.
This happened in the Netherlands 5 years ago and again happening this year. The 2nd and 3rd largest parties (in that case) simply refused on general principle to work together with the big winner and ended up creating a coalition with 3 more smaller parties that gave them a massive 1 seat majority in parliament (which they since lost as 2 of the MPs for those parties left their parties, taking those seats with them).
Thus the leader of the 3rd largest party ended up PM, as he was the leader of the largest block in the coalition rather than the largest block in parliament.
Mind that the resulting government often isn't very stable, as they have to make massive compromises working together and simply to get a majority in parliament. Dutch cabinet had basically to threaten to resign for every major vote in case they didn't get what they wanted, with their MPs knowing that'd likely mean they'd lose their cushy well paid jobs so they voted whatever was put in front of them...