Is confessing to a priest the only way to be forgiven? We first confess in our first communion and the priests say that we should always confess our sins, but is it the only way to be forgiven by God ?

Can't we pray and ask for forgiveness? I've heard of people confessing to a priest before dying.

Since the teaching on this may vary depending on the perspective of the answerer, please limit answers to those that specify the denominational view of the answerer, and make them good, supported answers.  A survey of the answer from various perspectives would be even better, as it will give information from the perspectives of a wider audience.
What is confession? St. John wrote:


  If we say we have no sin, we deceive ourselves, and the truth is not
  in us. If we confess our sins, he is faithful and just to forgive us
  our sins and to cleanse us from all unrighteousness. If we say we have
  not sinned, we make him a liar, and his word is not in us (1 John 1:8-10, ESV).


The verb used for 'confess' is the first-person-plural form of ὁμολογέω (homologeo). The word ὁμολογέω literally means "to say the same thing." It can also mean to "agree with," "acknowledge," or "confess" (source).

People often tend to think of confession as apologizing for our sins, asking for forgiveness. Some as a result believe that they only receive forgiveness for sins of which they ask to be forgiven (and/or apologize for). But confession is less about asking forgiveness for specific sins as it is agreeing with God that our wrong deeds are indeed sinful. It is more of an acknowledgement that what God calls sin is actually sinful. For instance, if we lie, confession means acknowledging that lying is wrong and that you sinned against God. Otherwise, the truth is not in us (and we are living in a state of sin, i.e. unrepentance).

Concerning confessing our sins, St. James says:


  Therefore, confess your sins to one another and pray for one another,
  that you may be healed. The prayer of a righteous person has great
  power as it is working (James 5:16, ESV).


Clearly it is good to confess our sins to other Christians, indeed it is essential for our healing. 

Who is a priest? There are a few ways to look at this. For the sake of the Stack Exchange community, allow me to explain a couple of these views. But before I do that, let's start where (most) everyone agrees: 1 Peter 2:5-9 describes believers as a holy and royal priesthood. Thus all Christians are priests before God. Under the old covenant, the faithful needed a priest to approach God, but now under the new covenant this is not necessary. Because of Jesus' sacrifice, all believers can now approach God's throne with confidence (Hebrews 4:16). Most Christians agree with this thus far. Now here is where they start to differ (understand that this must be brief, I cannot offer an apologetic for the priesthood here, I am giving only a cursory explanation).

In ancient expressions of the Christian faith (Eastern Orthodox, Roman Catholic, some Anglicans and Lutherans, a few other groups), a priest is usually someone who has been ordained by other appointed Christian leaders for our good, and is an excellent person to whom we may confess our sins. In the early Church, Christians publicly confessed their sins to the entire congregation. This soon became impractical and appointed leaders (elders, who are spiritual fathers, cf. 1 Corinthians 4:15) began hearing people's confessions privately. Priests of course believe that Christians can confess their sins directly to God, but a spiritual father can also give advice and help a Christian heal by praying for them (James 5:16). This has to do with learning humility and acknowledging that we cannot heal ourselves. Some traditions go so far as to say that priests are appointed by God to stand "in the stead" of Christ, having the ability to forgive (or not forgive) sins on God's behalf (cf. "Office of the Keys," Matthew 16:19; John 20:23).

Most evangelical Protestant Christians do not have a functioning priesthood in the historic sense of the term, but they do have the equivalent (a pastor/elder) who will offer advice, but they do not generally view confession of sins to them as any different as to other believers. They do not believe that anyone should function as a mediator between a Christian and God and that the historic Christian priesthood violates this (cf. 1 Timothy 2:5).

So is confessing to a priest the only way to be forgiven? Regardless of which perspective of the priesthood that you adopt, the answer is no. First, remember the meaning of the word 'confession' that was previously discussed. If we die before we had a chance to confess a specific sin, that isn't going to send us straight to eternal torment. It is failure to acknowledge that our actions are sinful (saying a sinful action is not sinful, i.e. not saying the same thing as God) that will jeopardize our relationship with God (this is calling God a liar according to St. John). Even traditions which practice the historic priesthood do not believe that this is the only way to be forgiven (but they do believe that it is a divinely appointed means of forgiveness).