Can Sketch copy moves that was used on the previous Pokémon, and does this differ over generations?

According to Bulbapedia:

2nd Generation

Sketch allows the user to learn the target's last move permanently, replacing Sketch in the process.

So, apparently, Sketch just copies the last move used, it doesn't matter if one of your Pokemon was sent KO or who was the target of that move.

3rd Generation

Sketch can copy a move a Pokémon attempted to use, but failed to due to sleep.

This means you will learn the last move attempted to use, not the last that was actually completed.

Note: Remember that Smeargle must be faster than the opponent in order to guarantee a successful Sketch.