both your examples are correct. the differences between ago, before and earlier are quite small. For example, you wouldnt really say "i did it ago i went out" but "i did it before i went out" is correct english. You can also use "before" like this "I've done it before" you could also say "i did it earlier" but "i did it earlier i went out" is wrong.
ago can only really be used in examples like the one you wrote, or you can also use it in some questions like "how long ago was it that you last watched tv?"