So I figured it out myself, and it works for me, I don't know if that is the best solution, but does exactly what I needed. So I just post this, if someone is looking for something similar.
I am getting the closeButton of the NSPanel and perform than a action on it to close the panel exactly in the way it animated to open the panel.
NSButton *closeButton = [self.imagePanelController.previewPanel standardWindowButton:NSWindowCloseButton];
[closeButton setTarget:self];
[closeButton setAction:@selector(closePanel)];
- (void)closePanel
{
int calcX = self.window.frame.size.width - self.bounds.size.width;
int x = self.window.frame.origin.x + calcX;
int y = self.window.frame.origin.y;
int width = self.bounds.size.width;
int height = self.bounds.size.height;
[self.imagePanelController.previewPanel setFrame:NSRectFromCGRect(CGRectMake(x, y, width, height)) display:YES animate:YES];
[self.imagePanelController.previewPanel close];
}