The Shiny attribute on a pokemon can generated from anyway you would normally catch a pokemon, egg or in battle. The odds of this are 1 in 8,192 for battles. Some people believe that it must be the first time you encounter that particular species for the first time. There is no evidence to support this.
Starting with 4th generation games, the odds of receiving a shiny pokemon actually increase with the number of battles you're in if you use the PokeRadar. This is known as "chaining." Chaining has been shown to be effective up to chains of 40. You'll know it has worked because the grass will glow instead of shaking.
Additionally, 4th generation games provide for breeding between different nationalities of pokemon. This has been shown to increase the odds to 1 in 2,048 (a significant increase).
If you'd like to know more check out Serebii.