I want to know exactly till which layer in the OSI model the 127.0.0.1 (loopback) packet goes till it is returned to the upper layer. I read some where it goes till data link layer. but as i was told data link layer cannot see the ip address then how data link layer decide that the packet has to be send "up" instead of "down".
When you say "layer", you are probably thinking of the OSI model, also known as the seven-layer model.
However, real TCP/IP implementations don't actually fit the OSI model very well. The OSI model was designed by a standards committee 30 or 40 years ago, based on how the committee members thought network stacks ought to be designed -- but that was before the Internet and without the advantage of the experience we have today.
127.0.0.1 is an IP address, and the loopback functionality of 127.0.0.1 is associated with the IP protocol. The IP protocol can be thought of as most closely associated with the "network layer" (in the OSI model). So, you could say that the answer is "the network layer".
But, it's important to realize that this is a bit of a lie. In real operating systems, there's not necessarily a clean separation between the TCP stack and the IP stack; the two are closely hooked together. So, it's doesn't make sense to ask "the code that does loopback processing -- what layer is it at?". The code that does loopback processing could plausibly be considered part of the TCP/IP stack, and the TCP/IP stack combines elements of the network layer, the transport layer, and the session layer. Loopback processing might also be performed in a separate device driver that, from the operating system's perspective, looks like another device (just like another Ethernet card, but different), so one could even make an argument that the code is really part of the data link layer or physical layer. See how the seven-layer OSI model isn't really adding much insight here? The code is what the code is; trying to attach a label based on one of those seven layers is a human construct, one that in this case isn't a very good match to reality.
It's probably better to think of the OSI model as a plausible vocabulary for talking about network protocols, but it's less helpful when talking about the structure of networking code: the structure of the code doesn't necessarily correlate well to the seven layers of the OSI model.
TL;DR: the OSI model is a lie.
Simple answer: The 127.0.0.1 address is an IP layer concept, so it has to be handed to the IP layer for correct routing and so.
More complex answer: The 127.0.0.1 address might be common enough in practical use that upper layers of the TCP/IP stack in your particular operating system cut corners and handle it specially. Even your application might figure out it is supposed to talk to itself and never touch the TCP/IP stack.
No, the totally misguided (and finally dead and buried) OSI protocol stack has no relevance to any of this.