It is possible that a Prime Minister could win the general election (i.e., win the most seats) but lose in his/her own riding.
In that case, the Prime Minister would likely prevail upon a government backbencher in a so-called "safe seat" to resign, and the Prime Minister would run in the subsequent by-election. The Prime Minister would remain as Prime Minister during this procedure.
The Prime Minister could also interpret the result as the electorate signifying satisfaction with his/her party, but dissatisfaction with him/herself personally. In such a case, the Prime Minister might resign and the Governor General would then ask another government member to form a government. Given the ego of your typical Prime Minister, that scenario is not likely to develop unless the Prime Minister also loses the by-election in the safe seat.