Question: In a Parliamentary democracy, general elections appoint constituency representatives. Most such appointees are members of parties with internally determined leaders. One or more parties then form a government, with one of these party leaders the head of government.

However, it's theoretically possible that a party that enters or stays in government due to its electoral performance has to change leader, due to that individual losing their seat. (Party leaders typically come from safe seats to prevent this.)

Has this ever actually happened? I'd be interested if this happened in the UK, or failing that in any nation with a Westminster system.

Answer: 
That's incredibly rare as you mentioned, but it has happened before twice in Canada to the same Prime Minister though he remained Prime Minister.

Prime Minister William Lyon Mackenzie King lost his seat [in York North in 1925](https://en.wikipedia.org/wiki/Canadian_federal_election,_1925) and his seat [in Prince Albert in 1945](https://en.wikipedia.org/wiki/Canadian_federal_election,_1945) but went on to be Prime Minister on both occasions. However, his party lost their majority in Parliament on both occasions, though they won the by-election in 1925 and was the largest party in 1945.

---

Usually, leaders would only lose their seat if their party loses too, in which has occurred twice in Australia and once in the UK.

John Howard lost his seat, Bennelong, in the [2007 Australian federal election](https://en.wikipedia.org/wiki/Australian_federal_election,_2007) while Stanley Bruce lost his seat, Flinders, in the [1929 Australian federal election](https://en.wikipedia.org/wiki/Australian_federal_election,_1929).

In the UK, Arthur Balfour lost his seat in Manchester East after the [Conservative landslide defeat in 1906](https://en.wikipedia.org/wiki/United_Kingdom_general_election,_1906).

These examples are [referenced from The Guardian](https://www.theguardian.com/notesandqueries/query/0,,-199840,00.html).
I’d like to point out the answer about Canada was wrong. Sitting Prime Ministers have lost their seats four times. The first time was in 1921 when Arthur Meighen lost his seat and the Conservatives, who had been in power since 1911, lost the election to William Lyon Mackenzie King. He had to run in another safe seat to sit in Parliament. Meighen then became the only sitting PM to lose his seat and the election a second time, in the 1926 election.

Meighen won the election of 1925, since he won the most seats, but could not form a government. King became PM when he formed a coalition with the Progressive party. Scandal forced him to resign, and a constitutional crisis was triggered when the Governor-General, Lord Byng, refused to allow a new election and invited Meighen to try to form a government. It lasted 88 days, and Meighen lost his seat in the next election and King and the Liberals formed the government again.

King lost his seat in the 1925 election, so he was the 2nd sitting PM to lose his seat, but still put together a coalition to form a government and was elected in a by-election in a safe seat after a resignation.

The 3rd time was Meighen’s loss in the 1926 election, and the fourth was King’s 1945 loss.