There's too little information in the question to be sure, so I can't definitively answer what happened. Rather I'll show how to investigate this and give some plausible cases.
I pressed some combination of letters by mistake
Once you realize that, press
F1 l or
C-h l (that's lowercase ell) to invoke the command
view-lossage. This displays the last 300 keys you pressed as well as other input events such as mouse clicks. Since Emacs 25, this also shows the commands that they invoked. In earlier versions, you have to use
C-h k to find what command a key or key sequence invokes, and that can give wrong results if you've changed some setting or moved to some buffer since that event, or for mouse events if you click in a different place.
If Emacs is behaving strangely, check the mode line (the line just above the bottom line, or at the bottom of the current window, that displays information such as the file name and line number) and the minibuffer (the line at the bottom where commands prompt for input). Maybe you switched to the buffer while some command (e.g. a search) was in progress? If the mode indication in the mode line has square brackets around the parentheses (e.g.
[(Python …)] instead of the normal
(Python …)), it means that a recursive edit is in progress, i.e. some command lets you edit the buffer. To exit the recursive edit and resume the command that started it, press
C-M-c. To exit the recursive edit and abort the command that started it, press
ESC ESC ESC. If there's a prompt in the minibuffer, click there to switch back to that prompt.
If you want to know why a particular character is in a particular color or font, move the cursor to that character and run
M-x describe-text-properties. This command is also available from the menu ‘Edit’ → ‘Text Properties’ → ‘Describe Properties’ or
C-mouse-2 ‘Text Properties’ → ‘Describe Properties’. Typically text formatting is done through faces and the face's documentation should at least indicate which package is responsible for it.
M-x list-faces-display (available as ‘Display Faces’ in the ‘Text Properties’ menu) shows the available faces. With the default light background theme, two faces use a purple background that matches the image in your question:
query-replace, both used to highlight matching text during a search command. I don't know how you could have reached that my mistake, but based on the colors, it's the most plausible scenario.
Emacs has a built-in package to highlight things temporarily. It leaves a discrete clue in the modeline: the minor mode indicator
Although anything could happen depending on what packages you have installed, highlighting is likely to be a per-buffer configuration. So if you can't figure out what's going on, it should be enough to close the file and reopen it.