Q:

How to improve/train my memory while playing long pieces on the piano? 

This question might result too broad and subjective to answer, so apologize first, but I'll try to make it as specific as possible. 
Recently I had a piano exam, consisting in three pieces: Partita No.  6 in E minor BMW 830, Brahms Rhapsody op.  70 No.1 in B minor and Beethoven's Sonata No.  15 (Pastoral).  
I had to play one of the pieces by heart, and I chose the Partita.  Fortunately, I played it well enough to pass the exam, but I still have the feeling that my memory wasn't that good at playing, that is, there were certain parts where I knew exactly which notes I was playing, as if I could write them all on a partiture, but there were others where I simply had to take a chance and trust my muscle memory to play the right notes.  That's the kind of playing I'd like to avoid, specially when playing long pieces, since the probability of missing a note or simply go blank increases.  
That hasn't caused me any troubles yet, but I'd like to continue my piano studies further, and I know it is the kind of thing I need to stop or control, since there will be times when my muscle memory fail.  My question then is

How can I train my memory, specially when playing long or difficult pieces like fugues or suites, so I don't have to rely on my muscle memory to play them by heart? 

A:

Playing something 'by heart' and 'muscle memory' to me seem to be intertwined.  With something physical, such as playing a piece, they cannot be separated.  Repetition (funnily enough, the French for rehearsal) will produce the muscle memory, and simultaneously, make the brain remember.  
For me, a person 'knows' something when it's possible to do something else at the same time.  E.g. play a C# melodic minor scale while saying the 2x table.  I haven't found a better way to achieve this level of 'knowing' without as much repetition as is necessary.  That may be a couple of hours for someone for some piece, or weeks of 8 hrs practice per day for a concert pianist who will perform with an orchestra sans sheet music. 
The understanding of a piece - its form and repeats, dynamics, fingering, etc.,  are all completed well before purely playing , playing , playing the piece will put it deep in your mind/body.  So deep that it's totally automatic.  That is NOT to say you ought to play in an exam automatically, but when a piece is that far inside, THEN you can actually put your own slant on it, should you wish.  And that slant can be very different for an exam or a public performance.  BUT - at that point, the choice is yours, because you've LEARNED it! 
An examiner I spoke with once said 'all I expect is that the candidate has prepared for the exam'.  A simple statement, but apposite - if you don't KNOW the piece well enough to play it in your sleep, you haven't played it enough times.  You don't look at the gearstick to see what gear you're in any more?  That's because you've done it enough times! 

A:

For me, the most reliable form of memory seems to be a mental aural replay of the piece.  Achieving a simple form of this is no harder than listening to a song on the radio a few times and then not being able to get it out of your head. 
To go from here to something that is reliable in performance takes more.  For one, there are more details to remember aurally, e.g., the sounds of the inner lines, or the way sections connect if there is repetition where the music takes different directions. 
The other important aspect is that you have to be able to go from what you hear to the right note on the keyboard.  This requires a lot of ear-training, to be able to "transcribe on the fly" in effect.  The more you know about harmonic structures, the more able you will be do with this. 
Two examples: a whiz of a pianist I knew used to learn scores primarily by reading them, away from the piano.  His ear-training was good enough that he could hear what he read, and his technique was such that playing the required scales and chords was also something that he could visualize.  I think included with this was a knowledge of the harmonic analysis and how it related to the form of the piece. 
Another example I read about was Johannes Brahms who would occasionally stun people by playing a composition of his a half-step higher or lower.  His knowledge of the piece (I believe) was such that he could hear it and recreated it, just like you or I can hear "Row, row, row your boat" and play it starting on any note.  For grins, I randomly take pieces I'm working on and try playing them in different keys "by ear".  I think this is a good exercise that can help strengthen the ability to play from memory. 
Part of getting there is something I recall Yo-Yo Ma saying his teacher would always tell him: "never play a note without hearing it first."  Easier said than done for those of us who are impatient. 

A:

If you’re like most pianists, your ability to memorize music is probably fine, but your ability to recall what you’ve memorized is affected by new and stressful surroundings, such as unfamiliar feel and sound of an unfamiliar instrument, unfamiliar acoustics, peer pressure, heightened fear of making a mistake in public, or any number of things that are different from your normal practice surroundings.  It’s funny that we practice so many things, but we don’t practice performing, at least in those same performance conditions. 
Here are some general memorization tips that may help:

Obviously, learn the music thoroughly to the point that the chance of having memory problems during practice is virtually nil.  This alone will give you the most confidence, and will prevent most memory and nerve problems. 
Practice with 100% of your mental energy.  Ten minutes of focused, disciplined practice is better than one hour of undisciplined repetition.  Take frequent breaks so that your mind and fingers reset often, which further enhances muscle memory. 
Commit all the technically difficult passages to muscle memory.  Whatever technical challenges there are (scales, arpeggios, interval jumps, etc.)  make sure you’ve mastered them to the point of muscle memory.  You can’t solve technical problems while performing. 
Practice very slowly, as much as half tempo.  This is harder than you think.  Sometimes we play the same things at the same tempo, muscle memory completely takes over, and then when just one thing goes wrong, we don’t know how to get back on track.  Practicing very slowly, forces your muscles and mind to exaggerate every thought and movement, and your ear hears the music in a new way, all of which further ingrains the motions into your mind and fingers. 
Practice without pedal.  Playing and hearing the same music you’ve played and heard a thousand times in this very different way helps you to hear and feel the notes in a way that makes it harder to forget how those notes feel and sound.  Again, this deliberate method makes you think more, which further helps you remember it. 
Play one hand while fingering the other.  This is ear training to help you mentally hear the “missing” notes.  This works wonders. 
Finger both hands without actually making any sounds.  Same as above. 
Play your pieces without warming up.  This helps you separate memory from technique. 
At potentially troubling spots, hold down the note(s) or chord(s) just before where you may have memory problems, and while holding those notes down, look at the keyboard and visualize the next few beats and sing them in your mind.  In other words, while you’re physically on the beat before the potential memory problem, imagine you physically playing the next few notes.  Sometimes when we have memory problems, the problem isn’t the place where you think it is, but getting from a previous place to that place where you start having the problems. 
Practice away from the piano.  Take the score, sit somewhere away from the piano, close your eyes, visualize a specific performance environment, and then imagine the sound and feeling of playing.  Check the score only occasionally for reference.  Sing out loud if that helps. 
Don’t rely too much on muscle memory.  Yes, that’s what you practice to achieve, but if you go off the rails even just a little bit in a stressful environment, there is no muscle memory to bring you back.  Practice potential memory failures by starting at odd measures, maybe significant milestones, so that no matter what happens, you know that you can jump to them.  Just the comfort of knowing you have this safety net can help you enough to not need them. 
Practice with the lid up, as on a performance piano.  The clarity of the overtones can be jarring if you're not used to it. 
On the days leading up to the performance, sleep well, eat well, and exercise.  On performance day, your goal should be to get you in the best physical and mental state for you to reproduce what you already know.  This isn't time to practice or solve new problems.  Over-practicing on performance days causes more problems than solves them.  Warm up, play enough of the music (preferably on the performance piano) to feel comfortable, and then relax as much as you can.  At performance time, remind yourself that you already know how to do this, remind yourself how much you love this music, and focus on convincing everyone around you that they should love this too.