"John gave to Mary" is a valid English sentence, but its meaning is unusual because "gave" is intransitive there. "Give" is usually transitive, so the reader expects a direct object: "John gave something to Mary."
The pronoun "what" stands in for the thing John gave to Mary. It's still transitive.
The whole noun phrase is "what John gave to Mary." That noun phrase can serve as the subject of its own sentence (e.g., "What John gave to Mary is none of your business.") or as the complement (if that's the right word) in this sentence.