There was a issue raised at mame4all-pi repository that resolved the problem. Apparently there is code that rejects controllers with more than 6 axes.
Here is what I did. In a terminal, type
sudo nano home/pi/RetroPie/emulators/mame4all-pi/src/rpi/minimal.cpp
Comment out lines 168 to 174 so it appears as below.
// if (SDL_JoystickNumAxes(myjoy[i]) > 6)
// {
// SDL_JoystickClose(myjoy[i]);
// myjoy[i]=0;
// logerror("Error detected invalid joystick/keyboard\n");
// break;
// }
Save and exit.
Next you need to recompile the code so type in the terminal.
cd home/pi/RetroPie/emulators/mame4all-pi/
make
Reboot and your PS3 controller should now work.