Since there were two 40A circuits going to the garage junction box, does that mean that there are two sets of 3 wires, one set for each circuit? (i.e. black, red, green for 40A circuit #1, and another black, red, green for 40A circuit #2) ?
If that's the case, then what you could do is repurpose one of the wires from the other set as the neutral. This is legal and to code, but you have to mark the 4th wire with the proper color using tape. For example, take circuit #1's wires (black, red, green), and circuit #2's black wire and the 4 together can be made into a proper circuit for the 14-50 plug as follows:
1. Disconnect all 3 of the wires for circuit #2 at the breaker panel (remove black and red from the circuit #2 40A breaker, remove the green from the ground bar).
2. Tape white electrical tape to the black wire for circuit #2 on both ends (in the breaker box and in the garage junction box).
3. Connect this new black-marked-white wire to the neutral bar in the circuit breaker box.
4. Cap off the red and green wires from the old circuit #2 in the garage junction box, they will not be used.
5. Wire the 14-50 outlet using the 4 wires (black, red, green, and the new black-marked-white).
This gives you a 4-wire 40A circuit. Yes, it's legal and to code to use a 14-50 outlet on a 40A circuit. The reason this is allowed is because there is no NEMA outlet standard for a 40A outlet. To do this to code, the breaker must be 40A, and the wire must be the correct gauge for a 40A circuit, and the device plugged into the outlet must be a 40A or less device. This setup is very commonly done on residential electric ranges, which are typically 40A devices and they are plugged into a 14-50 outlet on a 40A circuit.