Strictly RAW, using the optional flanking rules from the DMG, this would not qualify for flanking.
The flanking rules read:
When a creature and at least one of its allies are adjacent to an enemy and on opposite sides or corners of the enemy’s space, they flank that enemy [...]
When in doubt about whether two creatures flank an enemy on a grid, trace an imaginary line between the centers of the creatures’ spaces. If the line passes through opposite sides or corners of the enemy’s space, the enemy is flanked.
It's unclear exactly what you mean by "on top of", so I've illustrated a few scenarios, including what is required for flanking. To be clear, the rules here are written in the context of two dimensions, so there is going to be some sense of "it's up to the DM" when applying to three dimensions, but as written, the rules can be applied in the same way when transitioning to a three dimensional context.
Here are three conceivable scenarios in two dimensions. In the first, we assume a character is literally on top of and sharing the space with the dragon; note, no matter where our ally outside the dragon's space goes can give us two points of intersection with the sides of the dragons space. In the second, the grappling character (center) is not on an opposite side from his allies. The third shows what is necessary for flanking.
FinallyNow, if we do permit flanking rules to apply in 3 dimensions, we still do not satisfy the necessary conditions: the imaginary line does not pass through opposite sides of the dragon's space.
And a side view of this scenario, in case you weren't convinced:
So what is necessary to get flanking with your ally that is literally on top of the dragon?
The dragon needs to fly up 5 feet and you ally needs to run up under the dragon, so that we have this scenario (side view of 3d):
Notice here that the line passes through the top and bottom of the dragon's space, and we have the "opposite sides" as required by the flanking rules.