I have been playing Saint Saens' Swan on my guitar for past few days and found that notes are sometimes out of key (D major in this case) but still sound very nice and full of tension to the ears. Are there any books/resources on this subject?
Lots of music will often include notes from outside the scale as a part of the piece. This can happen in a few different ways and for a few different reasons. Sometimes it's as simple as, "it sounded good, so I did it" but other times there are specific reasons and it accomplishes a specific action for the music.
Sometimes a song will change keys for different sections, which we refer to as a Modultation. This can allow a song to move around throughout different keys, which can produce some interesting results, such as a change of feel or vibe for a different section.
Sometimes we will change a note so that it will bring tension and provide a resolution to another place. A simple example of this is a Secondary Dominant. A dominant chord is often used to resolve to the tonic (the I chord); its tension is best resolved to the tonic. In D major, D would be your tonic and A7 would be your dominant chord. A Secondary Dominant will alter some notes in the scale to create a different dominant chord that would best resolve to another chord in the scale. So in D, we may place an E7 before an A chord to create additional tension that will resolve to the A.
We can also "borrow" chords from another key, typically a Parallel key (A major and A minor would be considered parallel). This brings a different sort of feel to a given chord that can add some nice variety to your harmonies. An example of this in D major would be using a Bb major chord. The Bb major comes from D minor and some people describe it as feeling "uplifting".
The general concept that you're looking for here is Music Theory. There are a lot of books on music theory out there and depending on what type of music you are playing, some books would be more appropriate than others. You can also learn a lot about theory from a teacher, which I recommend for anyone who intends to take music seriously, at least as a starting point. As Richard mentioned, theory can feel more like science, or math, and is not always appealing to everyone when learned from a book. You can certainly learn to conceptualize these notes that fall outside the key without reading a book or taking lessons but you do end up in a position of not being able to describe what you're talking about to anyone else.
I think of music theory as being two things: 1) An explanation of what is happening musically; 2) A language used to describe what is happening musically. Lots of people tend to think of theory as a set of rules, which is largely where it loses appeal for people, but I don't think of it that way at all. The only time I really think of it as a set of rules is when we're looking at specific types of music and trying to recreate it. So if I'm trying to compose some music that sounds like Mozart, I need to follow a certain set of rules to accomplish that sound. On the other hand, if I were to follow those rules and try to write some Jazz music, I'm going to have a lot of trouble, largely because the two different approaches conflict with each other in some aspects, such as voice leading.
It appears that this interests you enough to look into it further, since you bothered to ask here, so I would recommend pursuing some further knowledge. There are all kinds of books and classes but there are also free online resources. I would suggest seeking out a teacher if you can afford one, as they can provide a lot of insight and answers to your questions without needing a forum. You could also search google for "learn music theory" and you can find endless sites that offer theory explanations, as well as sites that offer music theory lessons.
There are composition books, but any resources that address things like this will always seem a bit on the pseudo-scientific side (at least, in my opinion).
Instead of books, here's what you should do! Determine the current key (D major, apparently) and figure out what those pitches are in the key. For instance, maybe it's a G#, meaning it's a raised scale degree 4. Maybe there's also a B-flat, a lowered scale degree 6. Figure out which alterations have the best effect on you, and try to implement those alterations into your own writing.
In my experience, you'll grow much more quickly that way than by trying to read it in a book.