A vassal gained a title and he became independent
I'm King.
A Count under a Duke vassal of mine, inherited a Duke-level title (Petty Kingdom)… and now he's independent.
I assumed that since King>Duke, he would still be my vassal, but this didn't happen.
Is that because he's not my direct vassal, and his liege was a Duke, ignoring that that Duke herself had a King liege?
Or would that have happened anyway for other reasons?
(maybe my Duke was a Grand Mayor actually, in case this changes anything, while the Count was a regular noble)
A character becomes independent when their rank-defining title grants them independence.
When a character gains a title which means a promotion for them (like in this case from count to duke), their allegiance will be reevaluated based on who is the liege of the new title. When the title is independent, they become independent. When the title belongs to another realm, they become a vassal of that realm.
You can use this to your own advantage: When you can arrange that a count(ess) of another kingdom gains a duchy in yours, they will become your vassal and will take their county with them.