user4050: There are some defensive ideas to get one's own king stalemated in order to not lose the game (mostly in the endgame, but sometimes also in the middle game).

Have (master) games been played where the only idea not to lose was to stalemate the opponent?
What about any legal position (which need not have occurred in a real game)?
What if you forget about the "only idea" requirement?

Edit: Although there is already a totally fine answer, I would not mind more answers using another patterns.

user4543: Here is an example of such a position.
[FEN "4k2K/7P/8/8/8/8/8/8 b - - 0 1"]
[startflipped ""]

The only ways to draw are Kf8 and Kf7, since both stop Kg8 and stalemate the opponent. This has surely been played in master games.
For a slightly more complicated pawn endgame see, here is a Anand - Kramnik game. Black has to stalemate White at the end of this game, or else they lose.

user1485: It's possible to build a bit on magd's answer. Consider the following position.
 [Title "White to move"] 
 [SetUp "1"]
 [FEN "8/8/8/8/8/1N6/pk1K4/8 w - - 0 1"]

 1. Na1! Kxa1 2. Kc1

The only way to draw is 1. Na1! Kxa1 2.Kc1 (or 2. Kc2) stalemate. I think there are some nice studies with this theme, but I can't find them right now. 

4050: <p>There are some defensive ideas to get one's own king stalemated in order to not lose the game (mostly in the endgame, but sometimes also in the middle game).</p>

<ol>
<li><p>Have (master) games been played where the only idea not to lose was to stalemate the <em>opponent</em>?</p></li>
<li><p>What about any legal position (which need not have occurred in a real game)?</p></li>
<li>What if you forget about the "only idea" requirement?</li>
</ol>

<p><strong>Edit:</strong> Although there is already a totally fine answer, I would not mind more answers using another patterns.</p>
 4543: <p>Here is an example of such a position.</p>

<pre><code>[FEN "4k2K/7P/8/8/8/8/8/8 b - - 0 1"]
[startflipped ""]
</code></pre>

<p>The only ways to draw are Kf8 and Kf7, since both stop Kg8 and stalemate the opponent. This has surely been played in master games.</p>

<p>For a slightly more complicated pawn endgame see, here is a <a href="https://www.chessgames.com/perl/chessgame?gid=1472881" rel="nofollow noreferrer">Anand - Kramnik</a> game. Black has to stalemate White at the end of this game, or else they lose.</p>
 1485: <p>It's possible to build a bit on magd's answer. Consider the following position.</p>

<pre><code> [Title "White to move"] 
 [SetUp "1"]
 [FEN "8/8/8/8/8/1N6/pk1K4/8 w - - 0 1"]

 1. Na1! Kxa1 2. Kc1
</code></pre>

<p>The only way to draw is <code>1. Na1! Kxa1 2.Kc1</code> (or <code>2. Kc2</code>) stalemate. I think there are some nice studies with this theme, but I can't find them right now. </p>
