The second query runs faster because it breaks the process in small steps using temp tables (#TBL1 and #TBL2) to avoids repeated calculating results.
In first query using DISTINCT, UNION ALL, and joining in one complex query makes SQL Server re-compute the results multiple times, that slows it down so by storing results in temp tables SQL Server can optimize and reuse them and leading to better performance.