Pastiche has been described as a work that imitates the style or character of the work of one or more other artists. It follows, then that fanfic is a subset of pastiche, mostly distinguished by the author not expecting to get paid.
Professionally written pastiche at its best includes a lot of interesting work; I would argue that almost everything in the Arthurian Mythos after
Chrétien de Troyes qualifies, as well as Shoggoths in Bloom, Slaves of Silver, Stross's Laundry novels and Rosencrantz and Guildenstern Are Dead.
Scalzi's Redshirts starts out as a snarky Star Trek parody, but quickly goes metafiction as the titular redshirts figure out about the ridiculously high attrition rate among everyone on the away teams who isn't the Captain, Science Officer, Chief Engineer or Lieutenant Kerensky. They struggle to find a way to escape their fate before the narrative kills them.
Besides Rosencrantz and Guildenstern, I am reminded of Stranger Than Fiction.