For the connections, I used a breadboard to hookup the LED's anode to pin 9 (PWM pin) and cathode to Ground through a resistor.
My doubt is, why do we have to use the resistor ?
When I tried the circuit without the resistor, I didn't get the fading affect but just a direct on-off LED mechanism.
What role does the resistor plays here ?
This question has been asked and answered an awful lot of times already, but will never be answered too often.
Some people think (to themselves know) they are helping by telling people that you do not need to use a resistor.
Those are wrong with that assumption, because it isn't anything more than that.
The resistor reduces the current through the LED.
A LED will not do any current limiting, and if it would then it will be lost already.
This means you need to protect the LED from burning itself.
But also the output of your Arduino isn't capable of outputting some infinite current.
As a matter of fact the allowed current per output is very limited, only some 20 mA.
If you fail to do that, the output might be damaged too (and sometimes more than that).
So if you keep connecting LEDs to Arduino outputs without any resistor, you will for sure damage both of them.
Without any resistors means that one could buy LEDs with built in resistor.
That doesn't mean you do not need a resistor, but the one you need might be inside the component package.
But you really need to go and look for those, and find them for the right voltage too.
As MAS3 says, an LED can only safely handle limited current, and also an Arduino pin is limited in the current that it can provide without internal damage to the chip. (An ATMega328P has an absolute maximum per pin of 40mA, but about 20mA is a nice, safe level.)
A series resistor ensures that neither of these 'safe working' limits is exceeded.
A typical standard LED can only handle about 20mA of current, and an Arduino output pin can comfortably supply a similar amount as mentioned.
To calculate the required resistor value, you need to know the Vf, (forward voltage), of the LED. With standard red and green LEDs, you can estimate this voltage to be about 1.8V to 2V.
For this exercise, we'll use 1.8V, aim for an LED current of about 20mA, and assume a 5V Arduino is being used.
Then, to calculate the resistor value required, R = (Vsupply - VLED) / I = (5V - 1.8V) / 20mA = 3.2V / 20mA = 160Ω. To keep current below the 20mA limit, we'll use the next higher standard resistor value, which is 180Ω.
So now we know that the LED current will be about 3.2V / 180Ω = 17.8mA - safe for both the Arduino and the LED.
And its worth noting the current through an LED is basically an exponential function of forward voltage,
doubling every 20mV (in theory at least), so 5V into a 2V LED will guarantee it is fried instantly (if the
supply has the ability to supply large currents).
Hence you always control the current to an LED, not the voltage. The amount of light is directly
proportional to the current anyway, why would you not want to set the current explicitly?
This is done by a series resistor, or by using a feeble power source (tiny button cells have enough
internal resistance to connect direct to an LED for instance), or by using a constant-current drive circuit.