Securing two sections of 'floating' molding together and to wall
I added some comments to get clarity on the solution, so I'm going to answer with some generic options you could combine or use individually based on your particular solution. These are roughly sorted "best to worst" in terms of my preference for creating the best finished look.


Remove and discard the piece on the left. If the wall behind it is not flat, replace it with a flexible molding (ie flexible polyurethane molding) - attach to the wall with construction adhesive and nails to hold in place until the adhesive cures.
If the gap is because it's too short, replace it with a correctly sized piece.
If you can remove both pieces but don't want to replace either, or the above options don't sound ideal, remove both pieces. Assuming they're wood or another material that will hold screws, use a pocket screw or two in the back of the pieces to hold that seam tightly together. Reinstall as if it was one single piece of molding.
Easiest, but ugliest: Leave as-is. Use paintable latex caulk to hide the seam. Paint afterwards.


Edited to add, based on your edit above: If you don't want to remove the pieces, there won't be much you can do. Construction adhesive (common brand: Liquid Nails) may hold it down but it will be challenging to get it in there without removing the piece from the wall. Similarly, if you had that piece off, you could put a drywall anchor in the wall, then use a trim screw to hold the piece down. You mentioned the trim is mdf, which doesn't hold fasteners well, so the pocket screw idea is out (although that would require the pieces to be off the wall as well.)

In short: Without removing or replacing the pieces, there may not be much to do other than try your best to hide it with painter's caulk.