Unfortunately, your idea would be likely to fall foul of 'sore loser' laws, enacted in most states with the general intention of preventing unsuccessful candidates in one party's primary from running either as a candidate of another party or as an independent candidate.
These laws have been enacted by most states in some form - according to Sore Loser Laws and Democratic Contestation (2011) - in all states apart from Connecticut, Iowa & New York. In addition, the article identifies Florida and Vermont as states where it is potentially possible to run as an independent, or 'no-party' candidate, as well as participating in a party primary. However, both of these states have deadlines for filing candidacy which would require the candidate to file as an independent candidate before the results of their chosen party's primary were known, which would presumably hurt their chances of winning the primary in the first place.
Assuming, then, that your hypothetical candidate was successful in contesting a congressional election as an independent in one of these states, joining the Democratic caucus seems to be as simple as registering as a Democrat. According to Rule 1 of the House of Representatives' Democratic Caucus Rules:
A. All Members of the House of Representatives, the Resident
Commissioner from Puerto Rico, and the Delegates from American Samoa,
the District of Columbia, Guam, the Northern Mariana Islands, and the
Virgin Islands who are Members of the Democratic Party shall be prima
facie Members of the Democratic Caucus of the House of
Representatives.
This appears to also be the case in the Senate - for example, in 1955, when independent Senator Wayne Morse joined the Democrats, it seems to have been as simple as him registering as a member of that party. It is worth noting, however, that Morse was welcomed by the Democratic leadership - had he just defeated an official party candidate by frustrating the party's primary process, this would presumably be more of an issue, especially as the House Democrats' Caucus Rules also allow members to be expelled by a two-thirds majority vote.