Why does my singing sound OK when I'm playing the guitar, or doing scales, even though I don't have perfect pitch?
Because you store (memorize) and recall (sing) distances (relative pitch), not addresses (absolute/perfect pitch). The information you use is the amount of change in each note in relation to the last one, not the actual frequencies of every single note. This set of distances is, essentially, what a musical key is.
This dynamic is closer to relative pitch than it is to perfect pitch.
The skill used by singers to correctly sing a melody, following musical notation, by pitching each note in the melody according to its distance from the previous note.
From Music acquisition: effects of enculturation and formal training on development by Erin E. Hannon and Laurel J. Trainor:
Most adults encode and remember melodies in terms of relative
pitch (RP), that is, the pitch distances or intervals between notes of
the melody, rather than in terms of individual absolute pitches (AP).
Perfect pitch is the ability to identify or re-create a given musical note without a reference tone. With perfect pitch you can identify the name and/or frequency of a note by itself. But you don't need to know the frequencies of the notes to sing a song (unless you want to sing it in a specific key/note/frequency). What you need to know is the relationship between the notes. Something that is, for most of us, much simpler to memorize and sing.
When you sing with the guitar or sing scales, you have a reference tone. All the other notes are sang in reference to that one and each other. You are not singing each note as an individual, isolated, entity. For every note you sing, you have the note before it as reference (and maybe all the other notes, and the tonal center they create).
You might not recall those relationships as intervals, or know what a "major third" or "four semitones" is, but you are still able to store and recall those relationships, more abstractly, as a chain of movements/changes/distances, one related to the others.
All these apply to improvisation too. By now, you have stored that information from the major scale. You now know that set of distances. Given a reference chord or tone, you know which distances are allowed. In fact, that information might be so intensely carved in your brain that singing out of key might be even harder than singing on key.