I'm having some difficulty because even though the Transparent Sky option is just what I need, I don't want it for every pass in my scene - I need the sky to be rendered too in other render layers.
There is a BlenderArtists thread (can't find the URL at the moment) where the suggested solution was to duplicate the whole scene and have one of the scenes render with a transparent background, but that would surely kill my render time for this animation to have to build the BVH for a whole mountain landscape in two scenes.
There is a BSE question that somewhat addresses this kind of issue, but I believe the sky is not lighting the terrain. (It would be nice if I'm wrong... can't download the .blend to check) I need my terrain to be lit by the sky color.
Things I've tried include:
Setting a render pass with the entire material as a Holdout shader (but it just shows black without sky transparency checked).
Various combinations of render layers... but I can't get something that just serves as a mask between the landscape and sky.
I've considered using Material IDs or Object IDs, but concluded that it will cause problems for tree leaves on the mountain horizon, which rely on their texture alpha to look like leaves instead of square planes.
Update: Based on the answer suggested, I created a makeshift mask that separates the terrain (foreground) from the sky (background). Basically I used a Color Ramp to squash all non-black values to white. This might be a solution for some cases but it's certainly not elegant, as the horizon is not anti-aliased. Also if the sky is black (like space or a very dark night sky) it doesn't work at all.
Update 2: I figured out that I can make a sky mask by rendering a mist pass at extremely far away values (beyond all geometry) leaving just the sky white and all else black. This solves the anti-aliasing problem, the only drawback being that it prevents me from doing an actual mist pass in this same scene... so it's still not a total solution but good to know about.
Edit: Since posting the above screen shot I've realized the mist's Start distance doesn't need to be extremely far away - it can be any distance if the mist Depth is set to 0.00, and if given a closer Start value such as 0.0001 it may actually render faster than with a far away one (although a mist pass calculates quickly in any case).
Is there anything else I haven't thought of?